System disconnecting repeatedly

ssan7 years ago

Linux script to check if service is running and start if it's stopped.

Note: Tested for traccar 3.9 or above

#!/bin/bash
service=traccar

if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
sudo /opt/traccar/bin/startDaemon.sh
fi

Then add this above script in the crontab to run every minute.

  1. Open crontab using below command
    crontab -e
  2. Add line to run every minute.
          • /path/to/script > /dev/null

To receive email alert when service is started by cron, use following script(edit email id).

Note: This script uses mailx.
Command to install mailx on redhat distributions

yum install mailx

Command to install on debian based distributions

apt-get install bsd-mailx
#!/bin/bash

###edit the following
service=traccar
email=user@domain.com
###stop editing

host=<code>hostname -f</code>
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running"
else
 sudo /opt/traccar/bin/startDaemon.sh
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
subject="$service at $host has been started"
echo "$service at $host wasn't running and has been started" | mail -s "$subject" $email
else
subject="$service at $host is not running"
echo "$service at $host is stopped and cannot be started!!!" | mail -s "$subject" $email
fi
fi

For more information refer