I am migrating LTO-6 tapes to LTO-9 tapes. I wrote a script for this that uses parallel so that I can have two or 4 tape drives running at the same time. It works totally fine but the logging. I was trying to get a start date and time for each tape being migrated and a start date and time for each tape completing. I couldn't make this work, so I am logging the completed batch at the end of the loop. Is there a better way to do this so that I can log each "start" and "end" as it occurs while keeping the parallel command?
What I am trying to get is a list of tapes like this:
>>> STARTED 000123 at 2025-06-14_07:43
!!! FINISHED 000123 at 2025-06-14_12:20
>>> STARTED 000437 at 2025-06-14_07:43
!!! FINISHED 000437 at 2025-06-14_13:05
or
>>> STARTED 000123 at 2025-06-14_07:43
>>> STARTED 000437 at 2025-06-14_07:43
!!! FINISHED 000123 at 2025-06-14_12:20
!!! FINISHED 000437 at 2025-06-14_13:05
etc..
The scripts has several functions but the relevant one is this one:
function process1() {
VAR1=( $(cat ${LIST1})
if [[ ${VAR1[@]} ]]; then
for i in "${VAR1[@]}"; do
DATE=$(date +%Y-%m-%d_%R)
echo ">> STARTED" ${i} at $DATE >> /scratch/Migrate/job1.daily.progress
# line to convert LTO tapes
sem -j${JOB} --id my_id1 fsmedcopy -T ANTF -s LTO6_Pool_a -v LTO9_Pool_4 -G y -r ${i}
echo -e "Active tape ${i}"
done
sem --wait --id my_id1
DATE=$(date +%Y-%m-%d_%R)
echo "!!! FINISHED JOB" ${LIST1} at $DATE >> /scratch/Migrate/job1.daily.progress
echo "" >> /scratch/Migrate/job1.daily.progress
fi
exit
}
What I get instead is this:
>>> STARTED 000123 at 2025-06-14_07:43
>>> STARTED 000437 at 2025-06-14_07:43
!!! FINISHED 000123 at 2025-06-14_07:43
!!! FINISHED 000437 at 2025-06-14_07:43
Notice that both the "STARTED" and "FINISHED" ran at the same time when the loop ran. It should have logged the FINISHED when the process was completed. I believe this is because sem runs in the background and doesn't wait for the command to execute.
echocalls generate ... and assuming the primary goal is to have multiple processes appending to a single log file (eg,job1.daily.progress) ... and assuming you don't want the messages in the log file to get garbled/scrambled (eg, 2+ processes writing to the log file at the same exact time) ... I'd probably look at something as simple as usingflockto manage writes to the log file ....flockcoding more manageable I'd suggest a single function to handles all write activity to the log file; this limits allflockcoding to just that one piece of code (ie, theflockfunction); in the main processes/functions you would replace the currentechocalls with calls to theflockfunction (passing the desired message as an argument to the function); for high volumes of write activity (from multiple processes) this approach could lead to some delay/contention on writes (to the log file), but for occasional writes there should be no delay/contention issuessemin front of yourechoto ensure only one process writes at a time just the same as you usesemin front of your tape-related commands. Alternatively, you could write to the system log with theloggercommand.logger. Seeman rsyslogdandman logger.parShellCheck.shsample script.