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?

2

1 Answer 1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.