Linux – How to stop script after certain maximum time

bashcronlinux

I am doing daily backups to S3. I would only like to consume upload bandwidth only from 8 PM – 8 AM. Any individual run could take longer than 12 hours because our upload is crappy. I'm not worried about killing s3cmd arbitrarily because it will correctly recover on the next run.

My crontab would look like this:

# 0 20 * * * s3cmd sync FOO s3://MyBucket

How do I test and make it stop if it is 8 AM. Ideally, I wish I could do something like:

# 0 20 * * * time --limit 12H "s3cmd sync FOO s3://MyBucket"

I am using Ubuntu Server.

Thank!

EDIT:

Based on quanta's answer. For multiple upload commands I think I should do?

# 0 20 * * * s3cmd sync FOO s3://MyBucket; s3cmd sync BAR s3://MyBucket; s3cmd sync FOOBAR s3://MyBucket;
# 0 8 * * * pkill s3cmd; sleep 1; pkill s3cmd; sleep 1; pkill s3cmd;

?

EDIT:

NM, I like timeout better.

EDIT:

Here is a weakness of the timeout solution. If I have 12 backups to do and a 12 hour window to do it, I don't necessarily need to allocate 1 hour per backup. Some backups might take 5 hours, some 20 minutes. I don't care. All I care about is at 8 AM everything stops. I don't believe the timeout option allows this flexibility to adaptively set the timeout value. The kill option based on quanta's answer should be able to handle this.

Best Answer

you are likely wanting the timeout command.

timeout 12h s3cmd sync FOO s3://MyBucket

man timeout for more information.

at least in fedora this is installed with the "coreutils" package, which you should have by default.

Related Topic