Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain log for each run of a bash script

I have a bash script that does some data copy and some transformations. The script can run for several minutes depending on the file size. In the meantime, another process can trigger this script to copy its files.

I want to maintain individual log for each run of the script. Is it possible?

like image 328
Rajesh Reddy B Avatar asked Nov 22 '25 17:11

Rajesh Reddy B


1 Answers

Here is an example script(I created for an example) whose logs will be written to a log file based on date and time each time it runs:

cat check_script.ksh
CURRENT_TIME=$(date +%d-%m-%Y-%H:%M-%S)
echo "Running copy command now..." > "$CURRENT_TIME.txt"
## cp command here......
echo "Running another comand now....." >> "$CURRENT_TIME.txt"
## Bla bla bla
echo "Scrpit is completed..."  >> "$CURRENT_TIME.txt"

EDIT: Also to check either your script is already running or not, let's do the OLD school logic create Watch Dog file(a lock file kind of) at the starting of script(if it is already not created, put check first if it is NOT created or present then create it or exit the program considering that it is running) and every time script completes(after your last successful command) you could delete it then, if your next runs come and sees that file is there it should exit from the script then.

like image 117
RavinderSingh13 Avatar answered Nov 24 '25 07:11

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!