systemctl的服务配置文件模板
[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=simple
User=sonarqube
Group=sonarqube
PermissionsStartOnly=true
ExecStart=/bin/nohup /opt/java/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /opt/sonarqube/lib/sonar-application-8.5.jar
StandardOutput=syslog
LimitNOFILE=131072
LimitNPROC=8192
TimeoutStartSec=5
Restart=always
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
systemctl enable test
上面的命令相当于在/etc/systemd/system目录添加一个符号链接,指向/usr/lib/systemd/system里面的test.service文件。
这是因为开机时,Systemd只执行 /etc/systemd/system目录里面的配置文件。这也意味着,如果把test.service文件不使用符号链接而是直接放到 /etc/systemd/system目录下,就可以实现开机启动。当然,在 /usr/lib/systemd/system如果没有放 test.service文件就用不了systemctl 命令来管理程序了!
原文链接:https://blog.csdn.net/qq_40903527/article/details/127670847
#启动test
systemctl start test
#停止test
systemctl stop test
#重启test
systemctl restart test
#查看状态test
systemctl status test
#如果需要开机启动
systemctl enable test
#如果需要取消开机启动
systemctl disable test
#杀进程,向正在运行的进程发出kill信号。
systemctl kill test
#查看test相关的service配置文件
systemctl cat test
##查看 multi-user.target 包含的所有服务
systemctl list-dependencies multi-user.target
#切换到另一个 target,shutdown.target 就是关机状态
sudo systemctl isolate shutdown.target
service的常用方式:
1.格式:service
打印指定服务
2.格式:service
启动指定的系统服务
3.格式:service
停止指定的系统服务
4.格式:service
重新启动指定的系统服务
5.格式:chkconfig --list
查看系统服务列表,以及每个服务的运行级别。
6.格式:chkconfig
设置指定服务
7.格式:chkconfig
设置指定服务
8.格式:ntsysv
以全屏幕文本界面设置服务开机时是否自动启动。
打开redis命令:service redis start
关闭redis命令:service redis stop
设为开机启动:chkconfig redis on
————————————————
nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示“and”的符号)到命令的尾部。
nohup 是 no hang up 的缩写,就是不挂断的意思。
nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。
在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中。
& : 指在后台运行
nohup : 不挂断的运行,注意并没有后台运行的功能,,就是指,用nohup运行命令可以使命令永久的执行下去,和用户终端没有关系,例如我们断开SSH连接都不会影响他的运行,注意了nohup没有后台运行的意思;&才是后台运行
&是指在后台运行,但当用户推出(挂起)的时候,命令自动也跟着退出
那么,我们可以巧妙的吧他们结合起来用就是
nohup COMMAND &
这样就能使命令永久的在后台执行
nohup python3 main.py >> main.log 2> &1 &
在上面的例子中,0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) ;
2>&1是将标准错误(2)重定向到标准输出(&1),标准输出(&1)再被重定向输入到myout.file文件中。例如nohup command > myout.file 2>error.txt & 那么错误内容会输出到error.txt文件中
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_40903527/article/details/127670847