I have create a bash script to:
Using this script and crontab it allows the script to be ran every x time checking to make sure the program is running.
Log into the user who will / has permission to run the script as well as the program. I shall use Azureus / Vuze as my example since this was the program I wrote it for. I also assume that you are using the downloaded version and not the one from the repository.
Create an empty file. I shall use $home as in your user directory e.g /home/danny
Paste the following script into the file:
#!/bin/bash
PROGRAM=’azureus’if ps ax | grep -v grep | grep $PROGRAM > /dev/null
then
echo “$PROGRAM PROGRAM running, everything is fine”
exit
else
echo “$PROGRAM is not running”
echo “Starting $PROGRAM”
DISPLAY=:0.0 /home/danny/vuze/azureus &>/tmp/azureus.outif ps ax | grep -v grep | grep $PROGRAM > /dev/null
then
echo “$PROGRAM PROGRAM running, everything is fine”
else
echo “Unable to start $PROGRAM”
fi
fi
Just to explain the script from top to bottom. If you are not interested skip this paragraph. The first line declares it’s a bash shell script and where bash can be found. PROGRAM is a variable holding the program name this can be changed to any other program. The if statement does a “ps” command which lists all the process running and doing other various pipes and filters down the list. If the program is found then it prints the message and exits. If it does not exist then it will print a message to say it’s not running and starting the program, The third line after the “else” statement is the command to start azureus. This can be changed to your relevant program. DISPLAY=:0.0
is only necessary if the program has a GUI. It runs the program what is usually the display on port 0, the one users usually see on the monitor. The remaining parts check if the program has started and prints the relevant message.
Save and close the file.
The script needs execute permissions. Run the following command to do this $ chmod +x /home/danny/myScript.sh
crontab is the Linux scheduler. It does things based on time. There are two was to set this up.
Install gnome-schedule:
$ sudo apt-get install gnome-schedule
Once installed, go to System > Preferences > Scheduled tasks
Click “New” from the toolbar and select “A task that launches recurrently”.
Give it a description and in the command field enter the path to the script. I untick the “No output” option so I can see any errors when we test the script later. Choose the appropriate “Time & Date” for often the script should be ran.
The problem with this method is the customizability of the schedule with every minute the smallest value. To enter a more suitable value click on the “Advanced” radio button and for every 10 minutes enter the */10
and leave all other boxes “*”(asterisk). Click the “Add” button to save the entry.
Highlight and click “Run Task” to test the script.
Whilst logged into the user who will run the schedule edit the crontab: $ crontab -e
By default on Ubuntu Nano is the default editor. I use this template in crontab:
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
This should help you with entering values but to run the script every 10 minutes the line should be */10 * * * * /home/danny/myScript.sh
Save and exit crontab.
The script can be customized to check and run any program. Now hopefully any program crashes or if someone closes the program you want running it will automatically start it up and keep it up.
Cron job to start / stop Azureus – can not start azureus