| Home » Categories » Multiple Categories |
How To Use a Simple Bash Script To Restart Server Programs |
|
Article Number: 204 | Rating: Unrated | Last Updated: Tue, Sep 23, 2025 at 11:30 PM
|
To ensure that the most imperative programs remain online as much as
possible (even after a server crash or reboot), one can create a simple
bash script to check if the program is running, and if it is not, to
launch it. By using cron to schedule the script to be executed on a
regular basis, we can make sure that program relaunches whenever it goes
down.
Bash ScriptThe first step in this process is to create the script itself. There are a variety of programs such as upstart, supervisor, and monit, that have the capability to start and monitor applications on a virtual private server in a very nuanced way— this bash script will simply provide an on switch. Below is a sample script that starts apache if it finds it off.nano launch.sh #!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/apache2 start > /dev/null
fiOnce you have saved the script, you must give it executable permissions in order to be able to run it:
chmod +x launch.shApache can be replaced with any required application. Should you want to set up the script for a variety of applications, you can create a new script for each one, placing it on its own line in the cron file. Cron SetupWith the script in hand, we need to set up the schedule on which it will run. The cron utility allows us to schedule at what intervals the script should execute. Start by opening up the cron file:crontab -eCron has a detailed explanation of how the timing system works at the beginning. Once you know how often you want the script to run, you can write in the corresponding line. The most often that the script can run in cron is every minute. Should you want to set up such a small increment, you can use this template: * * * * * ~/launch.shEvery five minutes would be set up like this: */5 * * * * ~/launch.sh |
Attachments
There are no attachments for this article.
|
Installing and Using the Vim Text Editor on a Cloud Server
Viewed 2954 times since Fri, Dec 27, 2013
How To Create a SSL Certificate on Apache for Debian 7
Viewed 2734 times since Fri, Dec 27, 2013
How To Use Dokku Plugins to Access Additional Functionality
Viewed 5032 times since Sun, Dec 29, 2013
How To Create, Remove, & Manage Tables in PostgreSQL on a Cloud Server
Viewed 3649 times since Mon, Dec 30, 2013
How To Create Nagios Plugins With Python On CentOS 6
Viewed 4080 times since Sat, Jan 4, 2014
How To Set Up Master Slave Replication in MySQL
Viewed 3735 times since Thu, Dec 26, 2013
How To Install (LEMP) nginx, MySQL, PHP stack on Arch Linux
Viewed 14028 times since Sun, Dec 29, 2013
How To Install Z Shell (zsh) on a Cloud Server
Viewed 3082 times since Fri, Dec 27, 2013
How To Set Up a Postfix Email Server with Dovecot: Dynamic Maildirs and LMTP
Viewed 3953 times since Mon, Dec 30, 2013
Geddy.JS: A No-Brainer MVC Node.js Framework
Viewed 3679 times since Sun, Dec 29, 2013
|
