Systemd Ubuntu用法
Systemd (System and Service Manager) 是Linux的fundamental building blocks suite。通过systemd你可以实现程序的开启,停止与重启。
系统要求
systemd需要系统
- CentOS 7 / RHEL 7
- Fedora 15 or newer
- Debian Jessie or newer
- Ubuntu Xenial or newer
在系统里面开机自动启动
您需要在/lib/systemd/system
新建一个service文件
sudo nano /lib/systemd/system/hello_env.service
[Unit]
Description=hello_env.js - making your environment variables rad
Documentation=https://example.com
After=network.target (开始start 在有network以后)
[Service]
Environment="PATH=/home/ubuntu/.nvm/versions/node/v8.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" (定义node npm sh位置)
WorkingDirectory=/home/ubuntu/casval-server
Environment=NODE_PORT=3001 (如果有environment parameter)
Type=simple (表示只能运行与启动)
User=ubuntu (给权限)
ExecStart=/usr/bin/node /home/ubuntu/hello_env.js (运行的命令)
Restart=on-failure (失败时候重启)
[Install]
WantedBy=multi-user.target
让systemctl
控制script
sudo systemctl daemon-reload
开启script
sudo systemctl start hello_env
停止script
sudo systemctl stop hello_env
重启script
sudo systemctl restart hello_env
看logo与status:
sudo systemctl status hello_env
设置启动项:
sudo systemtl enable hello_env
删除启动项:
sudo systemctl disable hello_env
启动多个service
在一个service里面启动
Type=oneshot 的时候 可以用多个ExecStart在一个service里面
在多个service里面启动
systemctl start parameter1.service parameter2.service
systemctl daemon-reload
systemctl enable foo@param1.service
systemctl enable foo@param2.service
Options
Option | Description |
---|---|
Type | Configures the process start-up type. One of: simple (default) – starts the service immediately. It is expected that the main process of the service is defined in ExecStart . forking – considers the service started up once the process forks and the parent has exited. oneshot – similar to simple, but it is expected that the process has to exit before systemd starts follow-up units (useful for scripts that do a single job and then exit). You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited. dbus – similar to simple, but considers the service started up when the main process gains a D-Bus name. notify – similar to simple, but considers the service started up only after it sends a special signal to systemd. idle – similar to simple, but the actual execution of the service binary is delayed until all jobs are finished. |
ExecStart | Commands with arguments to execute when the service is started. Type=oneshot enables specifying multiple custom commands that are then executed sequentially. ExecStartPre and ExecStartPost specify custom commands to be executed before and after ExecStart . |
ExecStop | Commands to execute to stop the service started via ExecStart. |
ExecReload | Commands to execute to trigger a configuration reload in the service. |
Restart | With this option enabled, the service shall be restarted when the service process exits, is killed, or a timeout is reached with the exception of a normal stop by the systemctl stop command. |
RemainAfterExit | If set to True, the service is considered active even when all its processes exited. Useful with Type=oneshot. Default value is False. |
Please read more at here. https://www.freedesktop.org/software/systemd/man/systemd.service.html