未加星标

Converting from sysvinit to systemd

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Background

Like most people, I have used sysvinit to control which services are run on boot for years. In recent years, a replacement for sysvinit called systemd has been developed and is now appearing in most linux distributions, such Debian and Raspbian.

Systemd is designed to start system processes in parallel to achieve faster boot times. The older SysVinit started processes sequentially, meaning everything has to wait for a slow service e.g. getting an IP address through DHCP to complete, not just those services that depend on it.

If you have a python script that you want to run every time on boot, you will have read about using rc.local. This is a simpler way of doing things than in my case where I use a full daemon mechanism. SysVinit is an old-fashioned and outdated way of doing things. This guide is aimed at users looking to migrate away from rc.local and SysVinit daemons to the new systemd way of working.

My sysvinit Python script

I have a Python script that runs as a daemon and starts up using the traditional double-forking method. In other words, you can control it using the traditional commands:

$ sudo ./myscript.py start $ sudo ./myscript.py stop $ sudo ./myscript.py restart $ sudo ./myscript.py status

With systemd, your script doesn’t need to contain any daemon boilerplate code at all to handle those actions. You don’t need to:

design your code to run as a daemon by double-forking read the start/stop/restart/status command line arguments keep a PID file in /var/run redirect stdout/stderr to a log file in /var/log

Systemd does all of the above for you, so all you need is your Python script as you would run it at the terminal and a simple .service file.

myscript.status file

To define your service, you need to create a .status file. This file should be stored in the directory /lib/systemd/system

Here is an example that assumes that your Python script needs network access:

[Unit] Description=My Script After=network-online.target syslog.target [Service]

Type=simple

WorkingDirectory=_CURDIR_

ExecStart=_CURDIR_/myscript.py

StandardOutput=syslog

StandardError=syslog

User=myuser

Group=mygroup

Restart=on-failure

[Install]

WantedBy=multi-user.target

You need to substitute _CURDIR_ with the full path of your script.

You can set the user/group that systemd runs your script as by changing myuser and mygroup to a suitable value. Leave these lines out completely if you want to run as root.

Any output from your program that would normally appear at the console, for example error messages or informational messages from a print() statement are set up to be stored by syslog so that you can still look at them if necessary.

If your script should fail for any reason, the Restart line tells systemd to restart your service.

Whenever you change or add a service file, it is necessary to get systemd to re-read it using the command:

$ sudo systemctl daemon-reload

Shebang line

Normally in a Python script, you would have the first line stating which version of Python to use (the ‘shebang’ line). In our case, to ensure smooth logging, we add the ‘-u’ switch to disable buffering on stdout/stderr which would delay any log messages from appearing.

#!env/bin/python -u Some useful commands Task Old SysVinit New Systemd Start service $ sudo service myscript.py start $ sudo systemctl start myscript Stop service $ sudo service myscript.py stop $ sudo systemctl stop myscript Restart service $ sudo service myscript.py restart $ sudo systemctl restart myscript View service status $ sudo service myscript.py status $ sudo systemctl status myscript Check log $ cat /var/log/myscript.log $ sudo journalctl -u myscript
$ cat /var/log/syslog | grep myscript Enable on boot $ sudo chkconfig myscript.py on $ sudo systemctl enable myscript Disable on boot $ sudo chkconfig myscript.py off $ sudo systemctl disable myscript

Conclusion

I hope there is enough here to help you migrate your Python script from sysvinit to systemd. I realise that systemd is a lot more powerful and complicated than sysvinit and that there is an awful lot more to know than what I have included here.

Remember the reasons for moving your code from Python 2 to Python3? I would say that a lot of the same reasons apply for the move from sysvinit to systemd.

If you stay with the old way of doing things, eventually both you and your code will become old and obsolete.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: PythonLinuxDebianCUDHCDHCP
分页:12
转载请注明
本文标题:Converting from sysvinit to systemd
本站链接:http://www.codesec.net/view/483256.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(33)