Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. case "$-" in
  2. *i*)
  3. interactive=1
  4. ;;
  5. *)
  6. not_interactive=1
  7. ;;
  8. esac
  9.  
  10. if [ -z "$PS1" ]; then
  11. not_interactive=1
  12. else
  13. interactive=1
  14. fi
  15.  
  16. if [ -t 0 ]; then
  17. interactive=1
  18. else
  19. non_interactive=1
  20. fi
  21.  
  22. CRONPID=$(ps ho %p -C cron)
  23. PPID=$(ps ho %P -p $$)
  24. if [ $CRONPID -eq $PPID ] ; then echo Cron is our parent. ; fi
  25.  
  26. PPID=$$ # start from current PID
  27. CRON_IS_PARENT=0
  28. CRONPID=$(ps ho %p -C cron)
  29. while [ $CRON_IS_PARENT -ne 1 ] && [ $PPID -ne 1 ] ; do
  30. PPID=$(ps ho %P -p $PPID)
  31. [ $CRONPID -eq $PPID ] && CRON_IS_PARENT=1
  32. done
  33.  
  34. # start from current PID
  35. MYPID=$$
  36. CRON_IS_PARENT=0
  37. # this might return a list of multiple PIDs
  38. CRONPIDS=$(ps ho %p -C crond)
  39.  
  40. CPID=$MYPID
  41. while [ $CRON_IS_PARENT -ne 1 ] && [ $CPID -ne 1 ] ; do
  42. CPID_STR=$(ps ho %P -p $CPID)
  43. # the ParentPID came up as a string with leading spaces
  44. # this will convert it to int
  45. CPID=$(($CPID_STR))
  46. # now loop the CRON PIDs and compare them with the CPID
  47. for CRONPID in $CRONPIDS ; do
  48. [ $CRONPID -eq $CPID ] && CRON_IS_PARENT=1
  49. # we could leave earlier but it's okay like that too
  50. done
  51. done
  52.  
  53. # now do whatever you want with the information
  54. if [ "$CRON_IS_PARENT" == "1" ]; then
  55. CRON_CALL="Y"
  56. else
  57. CRON_CALL="N"
  58. fi
  59.  
  60. echo "CRON Call: ${CRON_CALL}"
  61.  
  62. if [ "Z$(ps o comm="" -p $(ps o ppid="" -p $$))" == "Zcron" -o
  63. "Z$(ps o comm="" -p $(ps o ppid="" -p $(ps o ppid="" -p $$)))" == "Zcron" ]
  64. then
  65. echo "Called from cron"
  66. else
  67. echo "Not called from cron"
  68. fi
  69.  
  70. PPPID=`ps h -o ppid= $PPID`
  71.  
  72. P_COMMAND=`ps h -o %c $PPPID`
  73.  
  74. if [ "$P_COMMAND" == "cron" ]; then
  75. RUNNING_FROM_CRON=1
  76. fi
  77.  
  78. if [ "$RUNNING_FROM_CRON" == "1" ]; then
  79. ## do something when running from cron
  80. else
  81. ## do something when running from shell
  82. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement