Bu örneğimizde, bir dosyaya her 2 saniyede bir mesaj yazan bir PHP servis/kod kullanacağız. Kod veya sistem herhangi bir nedenden dolayı durursa, supervisord kodu tekrardan çalıştıracak.


Konfigürasyon dosyalarını kontrol etmek


Öncelikle supervisor'un yeni bir konfigürasyon dosyası bulup bulmadığını kontrol edelim.


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

Yukarıda da gördüğümüz gibi, herhangi bir konfigürasyon dosyası bulunmadı çünkü /etc/supervisor/conf.d/ klasörü şu anda boş.


PHP kodumuz


Aşağıdaki içerikle /var/www/html/hello-world/run.php dosyasını yaratalım. Bu kod çalıştığında öncelikle /var/www/html/hello-world/hello.log dosyasını yaratacak ve her 2 saniyede bir içine mesaj yazacak.


<?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++;
}

Konfigürasyon dosyası


$ 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

Konfigürasyon dosyasını yükleme


Bu işlemi yapmadan önce, /var/www/html/hello-world/hello.log dosyasının var olmadığına emin olalım. Yukarıda da belirttiğimiz gibi, kod çalışır çalışmaz dosya otomatik olarak yaratılacak.


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

Aşağıdaki komut ile supervisor'u konfigürasyon dosyamızı /etc/supervisor/conf.d/hello-world.conf okuması için bilgilendirelim.


$ sudo supervisorctl reread
hello-world: available

Aşağıdaki komut ile, konfigürasyon dosyamızdaki kriterlere göre, supervisor'a /var/www/html/hello-world/run.php dosyamızı çalıştırmasını isteyelim. Bu komut otomatik olarak /var/www/html/hello-world/hello-world_stderr.log ve /var/www/html/hello-world/hello-world_stdout.log log dosyalarını yaratacak.


$ sudo supervisorctl update
hello-world: added process group

Kontrol


İşlemin çalışıp çalışmadığını kontrol olalım.


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

Eğer işlemin ismini hello-world:hello-world_00 açacak olursak: hello-world(program name):hello-world_00(process name _ process no)


Bu da doğrulama amaçlı. Yukarıda ve aşağıda gördüğümüz gibi, işlemin numarası 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

Bu da yaratılan log dosyasına mesajların yazılıp yazılmadığını kontrol etmek için.


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

Test


Eğer 17495 numaralı işlemi durdurursak, supervisor otomatik olarak kodu/işlemi yeniden başlatacaktır.


# 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

Ben şu anda vagrant kullanıyorum, yani bilgisayarı kapatıp açma işlemini yapacağım. Öncelikle /var/www/html/hello-world/hello.log dosayasının içeriğini temizleyelim. Bilgisayar'ı yeniden başlattığımızda, kodun/işlemin yeniden çalışıp çalışmadığını göreceğiz.


# 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.

Supervisor'u başlattığımızda, kodumuz otomatik olarak başlayacak. Bunu internet tarayıcımızdan http://192.168.50.30:9001/ adresine giderek kontrol edelim.