The commands timeout and watch are great tools to have for reliable, on-the-fly process scheduling; especially in cases where cron isn't working properly and fixing it would take too long. I've included short descriptions of both commands and an example for you to play with.
The command timeout runs a process (command or script) for a period of time, when time runs out the command timeout sends the signal you specified to the process, and if the process is still running after a period of time the command timeout will send the process a KILL signal. The command timeout accepts intervals of seconds (s), minutes (m), hours (h) and days (d), for its period of time. The command timeout accepts all of the signals accepted by the command kill for its signal argument. Here's its command syntax:
timeout -s <signal> -k <time with suffix> <duration> <process> <process arguments>
The command watch periodically runs a process (command or script) for a period of seconds then displays the output of the process fullscreen, so that, the user can watch for differences. The command watch has interesting options for running processes at precise times and for dealing with exit errors from processes. When specifying the process for the command watch to run the process and its arguments have to enclosed in single quotes or double quotes. Here's its command syntax:
watch -n <interval of seconds> <process with arguments>
For example, in my post titled "TCP/UDP Whitelist Connection Script," I mentioned using cron to run the bash script "whitelist.sh" every couple of minutes to close active TCP/UDP servers and connections unknown to the user. Instead of playing with cron you could tell the command timeout to run watch and watch to run the bash script "whitelist.sh". In this example, I'm telling the command timeout to send the process watch the KILL signal after 4 hours, and if the process watch is still running after 245 minutes then send it another KILL signal. I'm also telling the command watch to run the bash script "whitelist.sh" located in my home directory every 120 seconds (or 2 minutes). Here's the comand to run:
timeout -s SIGKILL -k 245m 4h watch -n 120 /home/username/whitelist.sh
The command timeout runs a process (command or script) for a period of time, when time runs out the command timeout sends the signal you specified to the process, and if the process is still running after a period of time the command timeout will send the process a KILL signal. The command timeout accepts intervals of seconds (s), minutes (m), hours (h) and days (d), for its period of time. The command timeout accepts all of the signals accepted by the command kill for its signal argument. Here's its command syntax:
timeout -s <signal> -k <time with suffix> <duration> <process> <process arguments>
The command watch periodically runs a process (command or script) for a period of seconds then displays the output of the process fullscreen, so that, the user can watch for differences. The command watch has interesting options for running processes at precise times and for dealing with exit errors from processes. When specifying the process for the command watch to run the process and its arguments have to enclosed in single quotes or double quotes. Here's its command syntax:
watch -n <interval of seconds> <process with arguments>
For example, in my post titled "TCP/UDP Whitelist Connection Script," I mentioned using cron to run the bash script "whitelist.sh" every couple of minutes to close active TCP/UDP servers and connections unknown to the user. Instead of playing with cron you could tell the command timeout to run watch and watch to run the bash script "whitelist.sh". In this example, I'm telling the command timeout to send the process watch the KILL signal after 4 hours, and if the process watch is still running after 245 minutes then send it another KILL signal. I'm also telling the command watch to run the bash script "whitelist.sh" located in my home directory every 120 seconds (or 2 minutes). Here's the comand to run:
timeout -s SIGKILL -k 245m 4h watch -n 120 /home/username/whitelist.sh
Do you have a suggestion about how to improve this blog? Let's talk about it. Contact me at David.Brenner.Jr@Gmail.com or 720-584-5229.
Comments
Post a Comment
Comments to this blog will be reviewed within 72 hours. No trolling please