NigelS
NigelS

Reputation: 3

Find, stop and restart a process with bash script

I'm running 'motion' on a Raspberry Pi and need to periodically restart it (or other processes).

#!/bin/bash
# find and restart running process motion
sudo kill $(pgrep -f motion)
sleep 5
echo "restarting"
sudo motion -b -c ~/.motion/motion.conf

All lines work fine at the command line, but the script stops after successful completion of the 'kill' line. Same with several other ways of using 'kill' e.g. ps -ef | grep motion | grep -v grep | awk '{print $2}' | xargs sudo kill

The word 'Terminated' is just echoed to the screen and script stops. Is there a right way to find and stop a process in a bash script?

Upvotes: 0

Views: 188

Answers (0)

Related Questions