You can keep a script (like as PHP or JS, Java,…) always live by Supervisor.

To install Supervisor on Centos 8/ Rocky Linux:

dnf install supervisor
systemctl enable supervisord
systemctl start supervisord

On Centos 7 use “yum” instead of “dnf”.

Create a file myname.ini in folder /etc/supervisord.d/ with below content:

[program:my-first-program]
	process_name=%(program_name)s_%(process_num)02d
	command=php get-address-loop.php  --sleep=3 --tries=3
	directory=/home/domain.com/public_html/location/
	autostart=true
	autorestart=true
	user=root
	numprocs=5
	redirect_stderr=true
	stdout_logfile=/home/domain.com/public_html/location/supervisor.log
  • “numprocs” is number of”numprocs” is the number of concurrently active processes.
  • stdout_logfile is folder you want to save log file

Update data:

sudo supervisorctl reread
sudo supervisorctl update

Start program:

sudo supervisorctl start all

If just start my-first-program:

sudo supervisorctl my-first-program 
  • “my-first-program” is program name in file myname.conf

Check program status:

supervisorctl status

You can restart Supervisor to test script auto-start:

systemctl restart supervisord

then “supervisorctl status” again

You can also make it online with cronjob: systemctl start supervisord && supervisorctl start all

A best practice config for multi processors:

[program: myapp]
	process_name=%(program_name)s_%(process_num)02d
	environment=
		NODE_ENV="_production",
		APP_PORT=100%(process_num)02d,
		MONGODB_NAME= "myapp",
		MONGODB_HOST = "mongodb.myapp.com",
		MONGODB_PORT = 27017,
		KAFKA_CLIENT_ID = myapp%(process_num)02d,
		KAFKA_GROUP_ID = "myapp",
		KAFKA_BROKERS = "kafka.myapp.vn:9092"

	command= node dist/main --myapp%(process_num)02d
	directory=/home/myapp
	autostart=true
	autorestart=true
	user=root
	numprocs=5
	redirect_stderr=true
	stdout_logfile=/var/log/myapp/myapp%(process_num)02d.log
	logfile_maxbytes=5MB
	stdout_logfile_maxbytes=5MB
	logfile_backups=100
	stdout_logfile_backups=100
,

DMCA.com Protection Status


Leave a Reply

Your email address will not be published. Required fields are marked *