[英]Why can't timeout take effect for tcpdump run under sudo?
我想以 10 秒的時間限制運行tcpdump
命令。
timeout 10 sudo tcpdump -i eth0 -nn 'host 192.168.1.176'
它不會停止。 為什么這里的tcpdump
timeout
命令沒有生效?
問題是timeout
以您的用戶權限運行。 sudo
進程將權限提升到 root(或其他用戶),因此不允許timeout
向子進程發送 SIGTERM。 這可以用strace
(我的注釋以#
開頭,以及為了可讀性而使用空行):
user$ strace timeout 1 sudo sleep 5
# lots of irrelevant stuff
# here, timeout sets up the timer to get a signal when the child should be terminated
rt_sigprocmask(SIG_UNBLOCK, [ALRM], NULL, 8) = 0
timer_create(CLOCK_REALTIME, {sigev_value={sival_int=1889673072, sival_ptr=0x560c70a21f70}, sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}, [0]) = 0
timer_settime(0, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=1, tv_nsec=0}}, NULL) = 0
wait4(12320, 0x7ffdfeb0ef0c, 0, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
# the signal arrives
--- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_value={int=1889673072, ptr=0x560c70a21f70}} ---
# timeout tries to kill the child
kill(12320, SIGTERM) = -1 EPERM (Operation not permitted)
# and gets EPERM!
修復方法是也使用 root 權限運行超時。 以下將按預期工作:
user$ sudo timeout 1 sleep 5
當然,如果你已經是root,那么在命令行中將timeout 1
放在sudo
之前還是之后都沒有關系。
root$ sudo timeout 1 sleep 5
root$ timeout 1 sudo sleep 5
試試這個:
sudo timeout 10 tcpdump -i eth0 -nn 'host 192.168.1.176'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.