我试图在离开后的一段时间后打开监控摄像头。
我想象它是这样的:
sleep 60 && sudo service motion start &> /dev/null &
但是当我退出时后台任务似乎被删除了。有没有办法让它在我离开后留下来?还要拥有root权限吗?
编辑: 好吧,我最终创建了一个脚本,而不是使用单个命令,它看起来像这样:
#!/bin/bash
if (( $UID > 0 )); then
echo "Only root can run this script"
exit 1
fi
if [ -z $1 ]; then
TIMEOUT=30
else
TIMEOUT=$1
fi
sleep $TIMEOUT && service motion start &>/dev/null &
exit 0
答案 0 :(得分:1)
使用nohup:
sudo nohup service motion start &
答案 1 :(得分:0)
除nohup
外,您还可以使用screen
:
screen
sudo service motion start
然后键入CTRL+a
,然后键入d
以分离屏幕。
要重新连接,您只需使用:
screen -x
了解更多信息:
man screen
答案 2 :(得分:0)
这里有两个问题:
sudo
下执行,这可能很困难。以下是常见方法:
nohup sudo ( sleep 60; service motion start ) &
#Has issues, if sudo authentication is not complete till this point, since background process cannot prompt you for sudo password. Better to call `sudo true` or `sudo echo -n` or similar dumb command, under sudo before calling the actual command.
#You may be tempted to call sudo only for service command, instead of entire command.
However, there is a risk that if sleep interval is long enough, then sudo token may expire.
echo service motion start | sudo at now+1min
#The problem with this command is that the granularity is minute level, not seconds level. Otherwise, this looks clean to me.
根据您的要求,您可以选择其中任何一种,或其他答案中提到的任何其他机制。