Systemd Ubuntu用法

Systemd Ubuntu用法

Systemd (System and Service Manager) 是Linux的fundamental building blocks suite。通过systemd你可以实现程序的开启,停止与重启。

系统要求

systemd需要系统

  1. CentOS 7 / RHEL 7
  2. Fedora 15 or newer
  3. Debian Jessie or newer
  4. 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

OptionDescription
TypeConfigures 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.
ExecStartCommands 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.
ExecStopCommands to execute to stop the service started via ExecStart.
ExecReloadCommands to execute to trigger a configuration reload in the service.
RestartWith 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.
RemainAfterExitIf 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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值