开发者

How to replicate CTRL+C in a shell script?

I'm running a script that performs a command that lasts开发者_开发百科 too long, so I want to interrupt it. Any help? I've tried to search a lot, and I've been pointed to the kill command. Although, I can't get it to work. By the way, I'm using a Mac OS X. Thanks!


Assuming you have the process' PID, send it a SIGINT signal:

kill -SIGINT PID

If you don't have the PID you can try pkill or killall, but they're somewhat less safe; a PID is the only way to uniquely identify a process. Right after you spawn the other process the PID should be in $!, so you can save it then

process-that-takes-a-long-time &
pid=$!
# other stuff
kill -SIGINT $pid


timeout 10 your_command


To replicate Linux Ctrl+C on a terminal on Mac OS X, use ctrl+C! Its the same keystroke combination Worked for me (first Steps on Mac)

edit: I didnt read the script is running in background.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜