更为简单的做法:
nohup ffmpeg -re -i "rtsp://root:[email protected]" -vcodec copy -acodec aac -r 20 -b:v 500k -b:a 32k -f flv "rtmp://192.168.1.191/live/livestream" >/dev/null 2>&1 &
#上述命令是一个监控摄像头推流的操作,连续运行会生成体积较大的nohup.out 文件;这个处理方法的关键是 >/dev/null 2>&1 这一段。
下面的内容可以作为如何设置一个定时运行的 systemd 服务的办法。
1.编写脚本 rmout.sh
vim /etc/systemd/system/rmout.sh
#内容:
cp /dev/null /run/nohup.out
2.编写systemd service
vi /etc/systemd/system/rmout.service
#内容:
[Unit]
Description = remove the nohup.out file
[Service]
ExecStart =/bin/bash /etc/systemd/system/rmout.sh
Restart = no
Type = simple
[Install]
WantedBy = multi-user.target
3.编写定时运行的systemd timer
vi /etc/systemd/system/rmout.timer
#内容如下:
[Unit]
Description=Run a service every 10 minutes
[Timer]
OnBootSec=5m # 开机5分钟后运行
OnUnitActiveSec=10m # 每隔10分钟运行一次
Unit=rmout.service # 所要启动的服务的名称
[Install]
WantedBy=timers.target
注: 定时器的时间单位后缀:us(微秒), ms(毫秒), s(秒), m(分), h(时), d(天), w(周)。 无后缀则默认单位"秒"