1

I have a script as follows, and I am trying to run it as a root cron job. The script starts mycommand, which in turn starts another process. When a condition is met, I would like the script to kill mycommand and its child process before exiting. Now, this seems to work at the terminal, the child processes end when the condition is met. But when the script is started as a root cron job, mycommand continues to run until I kill it manually. What am I doing wrong?

#!/bin/bash


# function to trap and clean exit
function clean_exit
{
PGID=`ps xao pgid,command,pid | grep 'mycommand'| sed 's/ .*//'`
kill -9 -$PGID
echo "process group killed: "$PGID
}

trap clean_exit SIGINT SIGTERM EXIT


sudo mycommand $1 > output.txt &
while sleep 1; do
# check output.txt every second
if [ condition met]; then     
    exit 1; 
fi;
done

crontab line is as follows:

30 10 * * * bash path/to/myscript.sh 
srao
  • 111

0 Answers0