In this example, we're going to run a php script to write messages into a file in every 2 seconds. The script will be restarted by supervisord if it stops for any given reason or if the system goes down.


Checking configuration files


Check to see if supervisor finds any new config files.


$ sudo supervisorctl reread
No config updates to processes
$ sudo supervisorctl update

As you can see above, it didn't find any config files because /etc/supervisor/conf.d/ config directory is empty.


Create PHP script


Create /var/www/html/hello-world/run.php file with content below. When this script runs, it should create and write messages into /var/www/html/hello-world/hello.log file.


<?php

$i = 0;
$file = fopen('/var/www/html/hello-world/hello.log', 'w');

while (1) {
fwrite($file, $i.' - Hello @ '.date('H:i:s').PHP_EOL);
sleep(2);

$i++;
}

Create config


$ sudo nano /etc/supervisor/conf.d/hello-world.conf

[program:hello-world]
command=/usr/bin/php /var/www/html/hello-world/run.php -DFOREGROUND
directory=/var/www/html/hello-world
autostart=true
autorestart=true
startretries=5
user=vagrant
numprocs=1
startsecs=0
process_name=%(program_name)s_%(process_num)02d
stderr_logfile=/var/www/html/hello-world/%(program_name)s_stderr.log
stderr_logfile_maxbytes=10MB
stdout_logfile=/var/www/html/hello-world/%(program_name)s_stdout.log
stdout_logfile_maxbytes=10MB

Load config file


Before we do that, lets confirm that the /var/www/html/hello-world/hello.log doesn't exist yet. As we mentioned above, as soon as the script runs, it will be created automatically.


$ ls -l /var/www/html/hello-world/
total 4
-rw-r--r-- 1 501 dialout 172 Mar 4 2017 run.php

Let supervisor read our config file /etc/supervisor/conf.d/hello-world.conf to start our service/script.


$ sudo supervisorctl reread
hello-world: available

Let supervisor start our service/script /var/www/html/hello-world/run.php based on the config we prepared above. This will automatically create log files /var/www/html/hello-world/hello-world_stderr.log and /var/www/html/hello-world/hello-world_stdout.log.


$ sudo supervisorctl update
hello-world: added process group

Verify


Lets check if the process is running.


$ sudo supervisorctl
hello-world:hello-world_00 RUNNING pid 17443, uptime 0:00:44

If we're to explain process name hello-world:hello-world_00: hello-world(program name):hello-world_00(process name _ process no)


This is to confirm. As you can see above and below, the process ID is 17443.


$ ps aux | grep hello-world
vagrant 17443 0.0 0.4 194836 9844 ? S 19:41 0:00 /usr/bin/php /var/www/html/hello-world/run.php

This is to see if the log was actually being updated.


$ tail -f /var/www/html/hello-world/hello.log

Test


If you kill current process 17495, supervisor will start our service/script automatically again.


# Kill it
$ sudo kill -9 17495

# Check if it started again
vagrant@beta:~$ sudo supervisorctl
hello-world:hello-world_00 RUNNING pid 17639, uptime 0:00:23

I'm on vagrant so I'll pretend like I'm powering off the computer and starting it again. I cleared the content of /var/www/html/hello-world/hello.log file. Let's see if everything starts after turning on the computer.


# Power off computer
vagrant@beta:~$ logout
Connection to 127.0.0.1 closed.
inanzzz:beta inanzzz$ vagrant halt
==> other: Attempting graceful shutdown of VM...

# Turn computer on
inanzzz:beta inanzzz$ vagrant up

# Start supervisor
$ sudo service supervisor start
Starting supervisor: supervisord.

As soon as we start supervisor, our scripts starts as expected. Lets confirm in web interface as well so go to http://192.168.50.30:9001/.