Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case "$-" in
- *i*)
- interactive=1
- ;;
- *)
- not_interactive=1
- ;;
- esac
- if [ -z "$PS1" ]; then
- not_interactive=1
- else
- interactive=1
- fi
- if [ -t 0 ]; then
- interactive=1
- else
- non_interactive=1
- fi
- CRONPID=$(ps ho %p -C cron)
- PPID=$(ps ho %P -p $$)
- if [ $CRONPID -eq $PPID ] ; then echo Cron is our parent. ; fi
- PPID=$$ # start from current PID
- CRON_IS_PARENT=0
- CRONPID=$(ps ho %p -C cron)
- while [ $CRON_IS_PARENT -ne 1 ] && [ $PPID -ne 1 ] ; do
- PPID=$(ps ho %P -p $PPID)
- [ $CRONPID -eq $PPID ] && CRON_IS_PARENT=1
- done
- # start from current PID
- MYPID=$$
- CRON_IS_PARENT=0
- # this might return a list of multiple PIDs
- CRONPIDS=$(ps ho %p -C crond)
- CPID=$MYPID
- while [ $CRON_IS_PARENT -ne 1 ] && [ $CPID -ne 1 ] ; do
- CPID_STR=$(ps ho %P -p $CPID)
- # the ParentPID came up as a string with leading spaces
- # this will convert it to int
- CPID=$(($CPID_STR))
- # now loop the CRON PIDs and compare them with the CPID
- for CRONPID in $CRONPIDS ; do
- [ $CRONPID -eq $CPID ] && CRON_IS_PARENT=1
- # we could leave earlier but it's okay like that too
- done
- done
- # now do whatever you want with the information
- if [ "$CRON_IS_PARENT" == "1" ]; then
- CRON_CALL="Y"
- else
- CRON_CALL="N"
- fi
- echo "CRON Call: ${CRON_CALL}"
- if [ "Z$(ps o comm="" -p $(ps o ppid="" -p $$))" == "Zcron" -o
- "Z$(ps o comm="" -p $(ps o ppid="" -p $(ps o ppid="" -p $$)))" == "Zcron" ]
- then
- echo "Called from cron"
- else
- echo "Not called from cron"
- fi
- PPPID=`ps h -o ppid= $PPID`
- P_COMMAND=`ps h -o %c $PPPID`
- if [ "$P_COMMAND" == "cron" ]; then
- RUNNING_FROM_CRON=1
- fi
- if [ "$RUNNING_FROM_CRON" == "1" ]; then
- ## do something when running from cron
- else
- ## do something when running from shell
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement