There is a chance that you might need to involve using both of upstart and systemd to initialize your process. Mostly the first major benefit to get from starting process via upstart or systemd is to get automatic restart when either of your system or process dies. Yes, you have customers to serve, minimizing down-time as much as possible is better.
So let's see how we could configure these things up with both upstart, and systemd.
Scenario
Let's assume that you're root, and you want to execute your NodeJS-based program in the background which its main file locates at ~/my-process/index.js
.
The program itself also accepts 2 environment variables namely ENV_VAR1
and ENV_VAR2
.
You also want to log its output (included its error output) to /var/log/my-process.log
. As well, you want to log date-time of process starting and ending.
The important thing is that you want to have such program automatically restarts when it dies, or system reboots.
upstart
Configuration below is based on Linux Ubuntu 14.04. But it should be similar to other modern systems.
- Create
my-process.conf
file at/etc/init/
with the following content
description "My Process"
author "haxpor"
start on runlevel [2345]
respawn
script
exec ENV_VAR1='My 1st parameter' ENV_VAR2='My 2nd parameter' node ~/my-process/index.js >> /var/log/my-process.log
end script
pre-start script
echo "[`date`] my-process is starting" >> /var/log/my-process.log
end script
pre-stop script
echo "[`date`] my-process stopping" >> /var/log/my-process.log
end script
Notice that in exec ...
inside script
and end script
. This is one line. But you can substitute the following for multiple lines.
...
script
chdir ~/my-process
export ENV_VAR1='My 1st parameter'
export ENV_VAR2='My 2nd parameter'
exec node ~/my-process/index.js >> /var/log/my-process.log 2>&1
end script
...
- Check configuration file syntax by executing
init-checkconf /etc/init/my-process.conf
Then you should see that syntax is OK as follows.
File /etc/init/my-process.conf: syntax ok
- Reload configurations to let upstart knows our new configuration file.
Execute the following command.
initctl reload-configuration
If you didn't see any error, then it's alright.
- Start
my-process
by executing
start my-process
You will see the following output
my-process start/running, process 20341
-
Other commands
As well, you can executestart/stop/status/restart
command for such process in the same way you did in 4 i.e.stop my-process
. -
At this point when you try to reboot the system, you can see that such process will be automatically started. Or you can try to kill such process then it will be automatically restarted as well.
-
(Bonus) See status for all services by executing
initctl list
.
systemd
Configuration below is based on CentOS 7. But it should be similar to other modern systems.
- Create
my-process.service
file at/etc/systemd/system
with the following content.
[Unit]
Description=my-process
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
Environment=ENV_VAR1='My 1st parameter' ENV_VAR2='My 2nd parameter'
ExecStart=/usr/bin/node ~/my-process/index.js
Restart=always
[Install]
WantedBy=multi-user.target
Notice that we didn't specify ... >> /var/log/my-process.log 2>&1
as I found it has a problem with systemd. I think it's better to avoid it. But we're going to use journalctl
to see log output instead, keep reading.
- Check configuration by executing
systemd-analyze verify /etc/systemd/system/my-process.service
If there is no error, you should not see any error message. Anyway, the following output can be ignored, in case you see.
Configuration file /usr/lib/systemd/system/YDService.service is marked executable. Please remove executable permission bits. Proceeding anyway.
Binding to IPv6 address not available since kernel does not support IPv6.
[/usr/lib/systemd/system/rpcbind.socket:6] Failed to parse address value, ignoring: [::]:111
Above is OK.
Below is not OK.
Configuration file /usr/lib/systemd/system/YDService.service is marked executable. Please remove executable permission bits. Proceeding anyway.
Binding to IPv6 address not available since kernel does not support IPv6.
[/usr/lib/systemd/system/rpcbind.socket:6] Failed to parse address value, ignoring: [::]:111
[/etc/systemd/system/heatap-server-debug.service:15] Missing '='.
Error: org.freedesktop.DBus.Error.InconsistentMessage: Unit is not loaded properly: Bad message.
Failed to create heatap-server-debug.service/start: Bad message
- Restart daemon to let systemd knows your new configuration file. Execute the following
systemctl daemon-reload
You won't see any output if it's ok.
- Enable
my-process
service by executing
systemctl enable my-process
Notice that you can omit .service
- Start
my-process
service by executing
systemctl start my-process
-
Other commands
As well, you can executestart/stop/status/restart
command for such process in the same way you did in 4 i.e.systemctl stop my-process
. -
At this point when you try to reboot the system, you can see that such process will be automatically started. Or you can try to kill such process then it will be automatically restarted as well.
-
You can see log by executing
journalctl -u my-process
网友评论