Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

We have a CentOS 5 VPS over at slicehost. I have a couple of PHP scripts that operate email and stuff.

I need a way to start these php scripts automatically on machine's reboots/startups and have them run in the background. A bonus would be to have the ability to auto restart if the script is stopped.

thanks!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You could add a couple of startup scripts: Below is the basic of what I would use

#!/bin/bash
# chkconfig 2345 35 80
# Description: one line description
# I'd also insert a longer description of the
# scripts here.

# set any environment variables that might be needed here, too.
su - <target_user> /path/to/php /mypath/startupscript &

In keeping with least privilege, you'll want to replace with the user you want the script to run as. If you want it to be root, then your php interpreter will need to be setUID and owned by root, but then you can remove "su - " entirely.

share|improve this answer
    
thanks! I keep getting this error no my shell scripts: /bin/bash^M: bad interpreter: What's the ^M ? cheers –  Trident Splash Jul 1 '09 at 16:56
2  
This is caused by a carriage return on Windows being copied over to Linux. Run: dos2unix <filename> and it will convert line-endings to Unix. –  Kevin M Jul 1 '09 at 17:01

You could try adding the script to /etc/rc.local that should start them when you reboot.

php /mypath/startupscript &

If you need them to auto restart then you probably need to write out a pid file or something and monitor them with a cronjob. Not sure if using php for a long running system management process is that great of an idea.

share|improve this answer

Most *nix allow @reboot as the time/date in crontab to execute when the server restarts.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.