2

I guess I could have a cron job run every minute, or 5 minutes, and have it somehow check if the process was already running (not sure how to do that).

Would that be the best way or is there another like .startup script in my home directory that I could put a script in?

cwd
  • 2,763
  • 9
  • 33
  • 48

3 Answers3

4

The issue here seems to be that you don't have root access on this host to change the system startup scripts like /etc/rc.local. The idea of using a @reboot line in your user crontab is worth exploring. I haven't tried that as a normal user but it should work.

The other idea is as you say to run something in your user crontab every few minutes, check if your process is running, and restart if not. For example:

*/5 * * * * ps -u $USER | grep myscript >/dev/null || $HOME/bin/myscript

be very careful with this sort of thing - if you make a mistake you will end up starting an extra copy of your program every 5 minutes which could eventually cause a lot of problems.

Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52
  • reboot is only for privileged accounts. adding reboot in a user's crontab will give a permission denied, as crontab runs that command with the user's privilege, not with root. – w00t Jan 28 '11 at 08:00
  • Yeah that was kinda what I was thinking. Leads to DoS situations and the like. – Phil Hollenback Jan 28 '11 at 08:07
  • well said - i was not thinking about if they had access or not however... ;-) good catch – Glenn Kelley Jan 28 '11 at 08:56
  • I had to change grep to grep [m]yscript so that it won't find the grep process, but otherwise this works great. Thanks! – cwd Mar 14 '11 at 16:44
2

you could use chkconfig - or be lazy and simply just run in your crontab

@reboot command-goes-here

This link may help you a great deal (saves me typing it all out ... of course)

http://linuxhelp.blogspot.com/2006/04/enabling-and-disabling-services-during_01.html

Glenn Kelley
  • 1,294
  • 6
  • 10
0

Depending on your distribution (you did not mention it), you have a pretty wide area of choice: chkconfig, rcconf, /etc/(rc.d/)rc.local, symlinking startup scripts in init.d\rc.d - but all these require root privilege.

I am assuming you have no access to sudo. These leaves you with just one option, adding a script in crontab that checks if the process is running, and if it not, then start it up. You also gain a kind of availability enhancement (eg. if your process crashes, it is started up again).

My take on this is:

#!/bin/sh

proc=process_name
`ps aux > /tmp/.$proc; awk '/$proc/{print $2}' /tmp/.$proc > /tmp/.x` #if process is alive then copy its PID in .x
    if [ -s /tmp/.x ]; then    #-s file True if file exists and has a size greater than zero. 
            echo k   #.x is greater than zero => process is alive, all ok
    else
            /etc/init.d/daemon start _or_whatever_startup file_ #.x was zero, process dead.
    fi
`rm -f /tmp/.$proc && rm -f /tmp/.x`
w00t
  • 1,164
  • 3
  • 19
  • 35