0

I'm running python on my server. I want to add date and time info to my nohup log file's lines.

Log file content similar to what I want to do (inside log file):

09/09/2023 07:13 Traceback (most recent call last):
09/09/2023 07:13 //Some Error
09/09/2023 07:13 //Error...

Is it possible to do this? So how?

oera
  • 13

1 Answers1

0

Although not a very perfect solution, I did something like this. I created a .sh file and added this codes.

#!/bin/bash

Output File

output_file="nohup_output"

nohup command > "$output_file" 2>&1 &

while true do

if tail -n 1 "$output_file" | grep -qF "$current_datetime"; then
    sleep 1
else
    tail -n 0 -f "$output_file" | while read -r line; do
current_datetime=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "$current_datetime $line"
truncate -s 0 nohup_output # To delete inside the nohup_output file
done >> "$output_file.log"
fi

done

In this way, I got printouts containing the date and time as I wanted.

oera
  • 13