I’m trying to make a push notification work on my debian vps (apace2, mysql).
I use a php script from this tutorial (http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2).
Basically, the script is put in an infintive loop, that check a mysql table for new records every couple of seconds. The tutorial says it should be run as a background process.
// This script should be run as a background process on the server. It checks
// every few seconds for new messages in the database table push_queue and
// sends them to the Apple Push Notification Service.
//
// Usage: php push.php development &
So I have four questions.
-
How do I start the script from the terminal? What should I type? The script location on the server is:
/var/www/development_folder/scripts/push2/push.php
-
How can I kill it if I need to (without having to restart apace)?
-
Since the push notification is essential, I need a way to check if the script is running.
The code (from the tutorial) calls a function is something goes wrong:function fatalError($message) { writeToLog('Exiting with fatal error: ' . $message); exit; }
Maybe I can put something in there to restart the script? But It would also be nice to have a cron job or something that check every 5 minute or so if the script is running, and start it if it doens’t.
4 – Can I make the script automatically start after a apace or mysql restart? If the server crash or something else happens that need a apace restart?
Thanks a lot in advance
You could run the script with the following command:
The nohup means that that the command should not quit (it ignores hangup signal) when you e.g. close your terminal window. If you don’t care about this you could just start the process with “php /var/www/development_folder/scripts/push2/push.php &” instead. PS! nohup logs the script output to a file called nohup.out as default, if you do not want this, just add > /dev/null as I’ve done here. The & at the end means that the proccess will run in the background.
I would only recommend starting the push script like this while you test your code. The script should be run as a daemon at system-startup instead (see 4.) if it’s important that it runs all the time.
Just type
and you will get the processid (pid). It will look something like this:
The pid is the first number you’ll see. You can then run the following command to kill the script:
If you run ps ax | grep push.php again the process should now be gone.
I would recommend that you make a cronjob that checks if the php-script is running, and if not, starts it. You could do this with ps ax and grep checks inside your shell script. Something like this should do it:
If you want the script to start up after booting up the system you could make a file in /etc/init.d (e.g. /etc.init.d/mypushscript with something like this inside:
(You should probably have alot more in this file)
You would also need to run the following commands:
to make the script start at boot-time. I have not tested this so please do more research before making your own init script!