Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit shell script execution time?

I have a scheduled cron job (which is actually a shell script). I'd like to limit the script execution time as it can work unacceptably long. For some reason I can not limit the script execution time from inside the script. Actually, I want my system to force the task termination if it runs more than N hours. Please advise.

like image 709
Maksym Polshcha Avatar asked Aug 30 '12 10:08

Maksym Polshcha


People also ask

How do I run a shell script at a specific time?

From the interactive shell, you can enter the command you want to run at that time. If you want to run multiple commands, press enter after each command and type the command on the new at> prompt. Once you're done entering commands, press Ctrl-D on an empty at> prompt to exit the interactive shell.

How do you run a command for a limited time?

The Timelimit program runs a given command then terminates the process after a specified time using a given signal. It initially passes a warning signal, and then after a timeout, it sends the kill signal. Unlike the timeout option, Timelimit has more options such as killsig, warnsig, killtime, and warntime.

How do you pause a Bash script for 5 seconds?

sleep Command Bash Script Example Here is a simple example: #!/bin/bash echo "Hi, I'm sleeping for 5 seconds..." sleep 5 echo "all Done."

How do I run a script every 5 minutes in Linux?

tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes. Exit this file and save changes.


1 Answers

I'm responding by myself just because none answered.

0 * * * * timeout -s 9 3540 /path/to/your_command.sh

will send a SIGINT to your command if it hasn't completed in 59 minutes.

like image 52
Maksym Polshcha Avatar answered Sep 21 '22 15:09

Maksym Polshcha