7

I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.

I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.

The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.

Thanks in advance.

Braiam
  • 66,947
  • 30
  • 177
  • 264
Teque5
  • 290
  • 2
  • 3
  • 9
  • 1
    Does this answer your question? [How to run application for a set time in shell](https://askubuntu.com/questions/456405/how-to-run-application-for-a-set-time-in-shell) – Pablo Bianchi Apr 11 '20 at 17:53

4 Answers4

17

Why not use /usr/bin/timeout?

$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
waltinator
  • 35,099
  • 19
  • 57
  • 93
14

I've just wrote the following and it seems to work:

ping google.com& PID=$!; sleep 3; kill $PID

Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Adam Byrtek
  • 9,741
  • 2
  • 31
  • 27
  • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do... – Teque5 May 26 '11 at 18:54
  • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected. – Adam Byrtek May 26 '11 at 18:57
  • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though... – Teque5 May 26 '11 at 19:00
  • Just remove the backslash before `!`. Once more sorry for the confusion. – Adam Byrtek May 26 '11 at 19:09
3

A simple (and not much tested) version of hatimerun:

#!/bin/sh

help(){
    echo "Usage"  >&2
    echo "  $0 -t TIME COMMAND" >&2
    exit 1
}

TEMP=`getopt -o t: -n "$0" -- "$@"`

if [ $? != 0 ] ; then 
    help
fi

eval set -- "$TEMP"

while true ; do
    case "$1" in
    -t) timeout=$2; shift  2;;
    --) shift; break;;
    esac
done

if [ -z "$timeout" ]; then
 help
fi

cmd="$*"

$cmd&
echo "kill $!" | at now+$timeout

See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.

Another way is to use upstart instead of this script.

David Foerster
  • 35,754
  • 55
  • 92
  • 145
Florian Diesch
  • 86,013
  • 17
  • 224
  • 214
0

Using Perl:

perl -e "alarm 5; exec @ARGV" systemctl list-timers

Using timelimit command:

sudo apt install timelimit    
timelimit -t5 node app.js

Using timeout command:

timeout -sHUP time command

Rakib Fiha
  • 266
  • 1
  • 11
  • 2
    That's already in https://askubuntu.com/a/102328/760903 – Olorin Feb 07 '19 at 05:27
  • 1
    @Olorin now the post is edited by OP including another solutions as well as a sample of the `timeout` command. – αғsнιη Feb 11 '19 at 05:48
  • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. ```myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$``` For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error. – Rakib Fiha Feb 11 '19 at 07:00
  • sorry, I have no idea what's happening there – αғsнιη Feb 12 '19 at 05:22
  • `tput reset` solves this problem – Rakib Fiha May 12 '19 at 17:54