Bash Script Example : Shutdown on Job Completion (Final Release -Freebsd And Linux) [1 ed.]

BASHSCRIPT EXAMPLE : HOW TO SHUTDOWN / POWEROFF ON JOB COMPLETION For FreeBSD and as well as for Linux, both.

255 83 761KB

English Pages [1] Year 2020

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Bash Script Example : Shutdown on Job Completion (Final Release -Freebsd And Linux) [1 ed.]

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

BASHSCRIPT EXAMPLE FreeBSD as well as Linux

HOW TO SHUTDOWN / POWEROFF ON JOB COMPLETION Following example of a bash script is already tested on FreeBSD and Linux, both. It works just fine. It uses pgrep (sys-utils on FreeBSD and procps on Linux). Alternatively once can use pidof as well on Linux. #!/bin/sh # or #/usr/local/bin/bash For FreeBSD #!/bin/bash # For Linux # Creating a lock scriptname=$(basename $0) pidfile="/tmp/${scriptname}.lock" # lock it exec 200>$pidfile flock -n 200 || exit 1 pid=$$ echo $pid 1>&200 # ### SERVICE="mplayer" # watching a long video on mplayer cli. # CHECK=`pidof "$SERVICE"` # Also works fine. CHECK=`pgrep -f "$SERVICE"` if pgrep -f "$SERVICE" >/dev/null 2>&1 ; #if [ ! -z "$CHECK" ]; # Also works fine. then # echo "$SERVICE is running ..." sleep 300s else # echo "$SERVICE is NOT running." echo “Powering-off in next 3 minutes.” sleep 180s # it will work for sysVinit and systemd #release the lock sudo rm -f "$pidfile" sudo shutdown -p now # For sysVinit sudo shutdown -p +3 # sudo systemctl poweroff # For systemd (not tested) fi exit 0