-- TOC --
systemd中的d
,表示daemon,它也是kernel加载的1号进程。现代Linux发行版,基本都使用systemd来管理系统。
启动一个服务:
systemctl start firewalld
关闭一个服务:
systemctl stop firewalld
重启一个服务:
systemctl restart firewalld
查询服务的状态:
systemctl status firewalld
在开机时启用一个服务:
systemctl enable firewalld
在开机时禁用一个服务:
systemctl disable firewalld
查看服务是否开机启动:
systemctl is-enabled firewalld
查看已启动的服务列表:
systemctl list-unit-files | grep enabled
查看启动失败的服务列表:
systemctl --failed
# 重启系统
$ sudo systemctl reboot
# 关闭系统,切断电源
$ sudo systemctl poweroff
# CPU停止工作
$ sudo systemctl halt
# 暂停系统
$ sudo systemctl suspend
# 让系统进入冬眠状态
$ sudo systemctl hibernate
# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep
# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue
# 列出正在运行的 Unit
$ sudo systemctl list-units
# 列出所有Unit,包括没有找到配置文件的或者启动失败的
$ sudo systemctl list-units --all
# 列出所有没有运行的 Unit
$ sudo systemctl list-units --all --state=inactive
# 列出所有加载失败的 Unit
$ sudo systemctl list-units --failed
# 列出所有正在运行的、类型为 service 的 Unit
$ sudo systemctl list-units --type=service
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。Unit 一共分成12种。
首先创建service文件,比如:
$ cat /lib/systemd/system/start_all.service
[Unit]
Description=start_all -- cook
After=network.target
[Service]
ExecStart=/bin/python3.8 /home/xinlin/repos/cook/start.py
ExecReload=/bin/python3.8 /home/xinlin/repos/cook/start.py
[Install]
WantedBy=multi-user.target
Service部分默认的type为simple。
ExecStart对应systemctl start
命令。
ExecReload对应systemctl reload
命令。
要设置为自启动,使用systemctl enable xxxx.service
命令,取消自启动使用systemctl disable
。
$ sudo systemctl enable start_all.service
$ sudo systemctl disable start_all.service
enable命令的作用,是在/etc/systemd/system目录下面,给service文件建立一个符号链接,systemd启动的时候,会去读取这些符号链接,将它们启动。自启动的进程,都属于root,涉及到路径的代码或脚本,建议全部使用绝对地址!
下面是我写的自启动httpd的service文件,可以用,type用forking:
$ cat /lib/systemd/system/start_httpd.service
[Unit]
Description=start apache httpd
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache-2.4/bin/apachectl start
[Install]
WantedBy=multi-user.target
再来一个示例
[Unit]
Description=My Custom App
After=network.target
[Service]
User=foo
Group=foo
WorkingDirectory=/home/foo/projects/mydaemon
ExecStart=/usr/bin/local/python3.7 app.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
首先用ulimit命令查看是否会产生coredump文件,如果不会,修改配置。
当coredump文件生产后,使用coredumpctl
命令来获得转存的coredump文件。
显示最近的10个coredump文件:
$ coredumpctl -r -n10
TIME PID UID GID SIG COREFILE EXE SIZE
Wed 2023-02-15 17:15:31 CST 70061 1000 1000 SIGSEGV present /home/xinlin/repos/xinlib/build/test/memcpy_fast_test 52.4K
Tue 2023-02-14 17:12:35 CST 49381 1000 1000 SIGSEGV present /home/xinlin/repos/xinlib/build/test/memcpy_fast_test 52.6K
Sun 2023-02-12 22:03:34 CST 59339 1000 1000 SIGABRT missing /home/xinlin/repos/xinlib/build/test/byte_ring_buffer_test n/a
Sun 2023-02-12 18:18:26 CST 49738 1000 1000 SIGABRT missing /home/xinlin/repos/xinlib/build/test/memcpy_fast_test n/a
Sat 2023-02-11 15:38:20 CST 20849 1000 1000 SIGSEGV missing /home/xinlin/test/a.out n/a
Sat 2023-02-11 15:37:01 CST 20750 1000 1000 SIGSEGV missing /home/xinlin/test/a.out n/a
Sat 2023-02-11 15:36:51 CST 20731 1000 1000 SIGSEGV missing /home/xinlin/test/a.out n/a
Tue 2023-01-17 09:39:59 CST 18011 1000 1000 SIGSEGV missing /home/xinlin/repos/xinlib/build/test/memcpy_fast_test n/a
Tue 2023-01-17 09:38:39 CST 17937 1000 1000 SIGSEGV missing /home/xinlin/repos/xinlib/build/test/memcpy_fast_test n/a
Tue 2023-01-17 09:36:09 CST 17842 1000 1000 SIGSEGV missing /home/xinlin/repos/xinlib/build/test/memcpy_fast_test n/a
好多missing....说明,systemd会自动清理coredump文件,及时转存哦!
转存,70061是上面显示出来的PID:
$ coredumpctl dump 70061 -o my_coredump_file
PID: 70061 (memcpy_fast_tes)
UID: 1000 (xinlin)
GID: 1000 (xinlin)
Signal: 11 (SEGV)
Timestamp: Wed 2023-02-15 17:15:31 CST (22h ago)
Command Line: test/memcpy_fast_test
Executable: /home/xinlin/repos/xinlib/build/test/memcpy_fast_test
Control Group: /user.slice/user-1000.slice/user@1000.service/app.slice/app-org.gnome.Terminal.slice/vte-spawn-5c78438a-4623-4071-8ca1-e95aa14183fb.scope
Unit: user@1000.service
User Unit: vte-spawn-5c78438a-4623-4071-8ca1-e95aa14183fb.scope
Slice: user-1000.slice
Owner UID: 1000 (xinlin)
Boot ID: a5ecbb3cc8b9445285bbbb90a380c4a7
Machine ID: ac8ade9e182f4b0580c0d0e84d1b2ee2
Hostname: likecat
Storage: /var/lib/systemd/coredump/core.memcpy_fast_tes.1000.a5ecbb3cc8b9445285bbbb90a380c4a7.70061.1676452531000000.zst (present)
Disk Size: 52.4K
Message: Process 70061 (memcpy_fast_tes) of user 1000 dumped core.
Module /home/xinlin/repos/xinlib/build/test/memcpy_fast_test with build-id f660572473f85bcec9a19fd27b72343712b3b591
Module linux-vdso.so.1 with build-id e09c2d859d0bcca3cc4ad2a1f473b243838cf213
Module ld-linux-x86-64.so.2 with build-id 321b87738a8ee7220b3c045c0d4d93150be54470
Module libc.so.6 with build-id 8257ee907646e9b057197533d1e4ac8ede7a9c5c
Module libgcc_s.so.1 with build-id cbcf5689acb247f987a22375c392c19a530a85c0
Module libm.so.6 with build-id 7c51a65c054f2ddc34c8325d82a1150f580f0d91
Module libstdc++.so.6 with build-id 6ce2a41dee893f9ede3c63f9aadad3826220476e
Stack trace of thread 70061:
#0 0x0000000000411a84 n/a (/home/xinlin/repos/xinlib/build/test/memcpy_fast_test + 0x11a84)
#1 0x0000000000663401 n/a (n/a + 0x0)
ELF object binary architecture: AMD x86-64
此时,coredump文件就在当前路径下了。
本文链接:https://cs.pynote.net/sf/linux/sys/202112245/
-- EOF --
-- MORE --