Check if a program is running and if not, run it.

I’ve built myself an OwnCloud server. Unfortunately every time I take the server or it’s router down for maintenance the OwnCloud client program on my desktop dies. To take care of this I created a script that checks if the owncloud client is running and starts it if it is not. Th do this, open at text editor such as gedit and:

#!/bin/bash

process=owncloud
makerun="/usr/bin/owncloud"

if ps ax | grep -v grep | grep $process > /dev/null
then
exit
else
$makerun &
fi
exit

then save the file in your home directory. Make sure the name of the program (owncloud) you are checking on is not part of the filename you use here. Otherwise “grep” will see that filename and think owncloud is running and just quit. In my case I used the fiename:

oc_restart

Next make oc_restart executable. Open a terminal and:

chmod +x ~/oc_restart

Alternatively you can change under the properties of the oc_restart file using Nautilus. Finally use Scheduled Tasks to run /.oc_restart every hour.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: