Issue
I have the following script to keep track of pid for nohup -
#!/bin/bash
random=$RANDOM
file=nohup_$random
echo "PID and output will be stored in nohup_$random"
echo nohup "$@" &> $file &
nohup "$@" &> $file &
curpid=$!
echo "PID is "$curpid
pidfile=nohup_${random}_$curpid
echo "-- PID for this process is "$curpid >> $pidfile
echo "-- Running command "$@ >> $pidfile
echo "-- From location "$PWD >> $pidfile
Can I somehow make the script to show a notification after the process is over? (Or, may be, rename the file to rename $file ${file}_done
when $curpid
is completed?)
Solution
You can achieve this by adding another nohup
command to your script
nohup sh -c 'while ps -p $0 > /dev/null; do sleep 10; done && mv $1 $1_done' $curpid $file &>> $pidfile &
This will still exit your script, and it will be running a while loop checking if the process if running every 10 units of time. When the work is done it will rename the file name appending "done" to it.
Modified script looks like this
#!/bin/bash
random=$RANDOM
file=nohup_$random
echo "PID and output will be stored in nohup_$random"
echo nohup "$@" &> $file &
nohup "$@" &> $file &
curpid=$!
echo "PID is "$curpid
pidfile=nohup_${random}_$curpid
echo "-- PID for this process is "$curpid >> $pidfile
echo "-- Running command "$@ >> $pidfile
echo "-- From location "$PWD >> $pidfile
nohup sh -c 'while ps -p $0 > /dev/null; do sleep 10; done && mv $1 $1_done' $curpid $file &>> $pidfile &
Answered By - Naks