systemctl命令介绍
command | explain |
---|---|
systemctl --failed | 查看启动失败的服务列表 |
systemctl list-units | 查看活跃的单元 |
systemctl list-unit-files | grep enabled |
systemctl status ssh | 查看某个服务(这里是ssh)的状态(等信息) |
sudo systemctl enable http | 设置服务(这里是http)的开机自启动 |
sudo systemctl disable http | 关闭服务(这里是http)的开机自启动 |
systemctl is-enabled test.service | 是否启用test服务 |
创建服务
(base) ┌──(***㉿kali)-[/home]
└─$ sudo touch test.sh
(base) ┌──(***㉿kali)-[/home]
└─$ sudo chmod 777 test.sh
#!/bin/bash
echo "start test ...";
touch /aaa.txt
echo "test over ..."
- /etc/systemd/system$路径下创建 test.service
[Unit]
Description=httpd #当前配置文件的描述信息
# After=network.target #表示当前服务是在那个服务后面启动,一般定义为网络服务启动后启动
[Service]
Type=forking #定义启动类型
ExecStart=/bin/bash /home/test.sh #定义启动进程时执行的命令。 # /usr/local/apache/bin/apachectl start
# ExecReload=/usr/local/apache/bin/apachectl restart #重启服务时执行的命令
# ExecStop=/usr/local/apache/bin/apachectl stop #定义关闭进程时执行的命令。
# PrivateTmp=true #是否分配独立空间
[Install]
WantedBy=multi-user.target #表示多用户命令行状态
添加服务
(base) ┌──(***㉿kali)-[/etc/systemd/system]
└─$ sudo systemctl enable /etc/systemd/system/test.service
Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /etc/systemd/system/test.service.
结果
- 重启之后发现多了个失败的服务
更多:自动交互root登录
sudo apt-get install expect
#!/usr/bin/expect
echo "start test ...";
set password "-------"
spawn /usr/bin/sudo su
expect "***"
send "$password\n"
interact
sleep 10
gnome-terminal -t "roscore" -x bash -c "roscore;exec bash;"
echo "test over ..."
(base) ┌──(***㉿kali)-[/home]
└─$ ./test.sh
spawn /usr/bin/sudo su
[sudo] *** 的密码:
(base) ┌──(root㉿kali)-[/home]
└─#
#!/usr/bin/expect
set password "-------"
spawn /usr/bin/sudo su
expect "***"
send "$password\n"
expect "#"
send "touch aa.txt\n"
interact
更多:crontab 定时任务
命令生成
*/30 * * * * test.sh > /home/username/Desktop/test.txt
定时启动@reboot /home/username/test.sh
开机自启动@reboot sleep 300 && /home/test.sh
开机自启动
添加命令
(base) pdd@pdd-Dell-G15-5511:/etc/docker$ crontab -l
no crontab for pdd
(base) pdd@pdd-Dell-G15-5511:/etc/docker$ crontab -e
no crontab for pdd - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [1]: 2
ok
/etc/rc.d/rc.local 开机启动项命令
C&G
Linux系统中软件自启动的三种方式
linux 之.service文件简介
Crontab详细用法-定时任务详解
如何让shell脚本每天定时执行?
linux定时任务