Sometimes it is necessary to run a job in the foreground that would normally be run as an overnight cron job.
When the job is run in the foreground, slightly different code may be required. Rather than having two scripts, I thought of following:
#!/bin/ksh
TTY=$(tty)
if [ "a$TTY" = "anot a tty" ]
then
CRON="YES"
echo cron job
else
CRON="NO"
echo terminal session at $TTY
fi
If your script is invoked with standard input redirected from a file other than a TTY device or if the script is invoked in a pipeline (other than as the 1st command in the pipeline), the code will indicate that it is running as a cron job.
#!/bin/ksh
ps -ef |grep /etc/cron
ps -ef |grep etc/cron|read user cronpid discard
echo /etc/cron is $cronpid
ps -ef |grep $cronpid |grep $0|read user startpid discard
echo startpid is $startpid
if [ "a$startpid" = "a" ]
then
echo is not a cron job
exit 1
fi
echo $0
pid=$$
echo my process is $0 pid $pid
ps -ef |grep $0 |grep $startpid
ps -ef |grep $0 |grep $startpid|read user pid1 discard
echo $pid $pid1 should be same
if [ $pid -eq $pid1 ]
then
echo is cron job
else
echo is not cron job
fi