소개
Linux 시스템에서는 시작 및 중지를 systemd
편리하게 제어하고 自定义shell脚本
, 백그라운드 작업을 구성하고, 자동 시작 및 로그 리디렉션을 활성화하는 데 사용할 수 있습니다.
을(를 ) 사용하려면 systemd
먼저 설치해야 합니다.systemd
설치하다systemd
Linux 서버에 설치되어 있지 않은 경우 또는 다른 명령을 사용하여 설치할 systemd
수 있습니다 .yum
apt
systemd
# yum
yum install systemd
# apt
apt install systemd
사용자 정의 쉘 스크립트 편집hello.sh
vim /root/hello.sh
저장하려면 다음 내용을 입력하세요.
#!/bin/sh
while true
do
date +'%Y-%m-%d %H:%M:%S'
sleep 5
done
이 스크립트는 스크립트 실행 권한을 수정하기 위해 5초마다 현재 시간(2023-07-10 15:43:35 형식)만 출력합니다.
chmod +x /root/hello.sh
hello.service
파일 편집
$ vim /lib/systemd/system/frps.service
콘텐츠 작성
[Unit]
Description=Hello Service
After=network.target syslog.target
Wants=network.target
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/root/hello.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=hello
[Install]
WantedBy=multi-user.target
/etc/rsyslog.d/
새로 추가hello.conf
vim /etc/rsyslog.d/hello.conf
다음을 입력
if $programname == 'hello' then /root/hello.log
& stop
hello.service
StandardOutput, StandardError 및 SyslogIdentifier 구성 항목과 결합되어 hello.sh
출력이 파일로 리디렉션됩니다 /root/hello.log
.
재시작rsyslog
리디렉션 로그 구성은 다시 시작할 때까지 적용되지 않습니다 . 그렇지 않으면 로그가 또는 rsyslog
로 출력됩니다.
/var/log/syslog
/var/log/messages
systemctl restart rsyslog
알아채다
위의 구성은 centos 시스템에서 실제로 효과적입니다.
Ubuntu 시스템에서 다음 수정 사항을 바꿉니다.
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=hello
# 改成下面
StandardOutput=file:/root/hello.log
StandardError=file:/root/hello.log
systemd
명령 관리 사용hello
# 启动hello
systemctl start hello
# 停止hello
systemctl stop hello
# 重启hello
systemctl restart hello
# 查看hello状态
systemctl status hello
hello
전원을 켤 때 자동 시작 구성
systemctl enable hello