3

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?

4
  • Fairly possible, just write echo or print commands before your actual commands so that you could track where the script is on current time, let me know if this helps you, I will post same thing on answer then. Commented Feb 11, 2018 at 13:59
  • Also in case you need to refrain the same script to be running if another instance of script is running you could do this also. Please do let us know your complete query with samples so that we could help you on same. Commented Feb 11, 2018 at 14:02
  • @RavinderSingh13 I'm already doing this. I want an individual log file for each run. Commented Feb 11, 2018 at 14:02
  • Then you could save your log file as per date and time like output_of_script_11_FEB_2018_9AM.txt etc. or you meant something else? Commented Feb 11, 2018 at 14:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I will make some changes according to our requirement.
@RajeshReddyB, glad that it helped you. See my edit to see if your script is already in progress suggestion could help you sometime too.
0

You could write your logs to a file whose name is based on:

  • current date at the moment when you run the script
  • current PID of your program's instance

This should be enough to make sure each instance of your program has its own log file.

Here is a small example:

pid=$(echo $$) # Current instance PID
date=$(date +%s) # Seconds since Epoch
logfile="myscript.$date.$pid.log"
echo "I'm a log" > $logfile
echo "I'm another log" >> $logfile

If you want to include the name of the process that triggered your script, you could pass it as an argument to your script, like this:

myscript.sh

parent="$1"
pid=$(echo $$)
date=$(date +%s)
logfile="myscript.$date.$pid.$parent.log"
echo "I'm a log" > $logfile
echo "I'm another log" >> $logfile

You would call this script like this:

sh myscript.sh parent_script_name

And it would create a file similar to this one:

myscript.1518358314.85866.parent_script_name.log

3 Comments

Can i use the process name that triggered the script in the file name?
You would need to pass it as an argument to your script.
Thanks. I will make some changes according to our requirement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.