Script to start/stop jobs

Startup scripts samples.

Start

THIS DOES NOT QUITE WORK! The job starts but the pid is not the real pid! So the stop is also bad because of this.

#!/bin/bash
RUN_FILE=/var/run/somejob.pid
nohup some-job > /dev/null 2>&1 &
echo $! >> $RUN_FILE
exit

Stop

THIS DOES NOT QUITE WORK!

#!/bin/bash
RUN_FILE=/var/run/somejob.pid
for pids in `cat $RUN_FILE`;
do
  echo "Killing: $pids"
  kill -9 $pids
done

Restart

#!/bin/bash
/path/to/stop-job.bash
#sleep 5
/path/to/start-job.bash

Other scripts

Simple start

#!/bin/bash
HAPCOUNT=`pgrep haproxy|wc -l`
if [ $HAPCOUNT -eq 0 ]
then
  echo "Starting haproxy ..."
  haproxy -f /etc/haproxy/haproxy.cfg
else
  echo "haproxy already running"
fi
exit

Simple stop

#!/bin/bash
HAPCOUNT=`pgrep haproxy|wc -l`
if [ $HAPCOUNT -eq 0 ]
then
  echo "haproxy not running"
  exit
fi
#
for pids in `pgrep haproxy`;
do
  echo "Killing: $pids"
  kill -9 $pids
done
exit

QR Code
QR Code tech:linux:start_stop_script (generated for current page)