# Script to force job termination after a given time

**URL:** <https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877>\
**Category:** Users/Site Administrators\
**Created:** [February 23, 2018, 3:07pm UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877 "2018-02-23T15:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnaud](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@arnaud](https://community.openpbs.org/u/arnaud)\
**Post date:** [February 23, 2018, 3:07pm UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877/1 "2018-02-23T15:07:45Z")

</div>

I am using PBS on a cluster where PBS is configured as follows: if one submits a job with a given walltime and lets the job run for longer than the wall time, the scheduler will not kill the job. However, it will associate a ‘red flag’ with the user who did this, and give him low priority for his next job.

To avoid this, I would like to insert manually a job termination in my PBS script.

See for example the following basic script:

#!/bin/bash

#PBS -l walltime=00:01:00  
#PBS -l mem=1gb  
#PBS -l nodes=1:ppn=1  
#PBS -q batch

./program.o

How may I modify this script in such a way that the job is automatically killed with qdel after 1 minute?

---

<div class="post-metadata">

**Author:** ![adarsh](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@adarsh](https://community.openpbs.org/u/adarsh)\
**Post date:** [February 26, 2018, 8:37am UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877/2 "2018-02-26T08:37:12Z")

</div>

If you submit the above script and if program.o runs for more than 1 minute, then it will be automatically killed by the pbs\_mom, as the job has exceeded the requested walltime of 1 minute. You can see this message in the log file

02/26/2018 08:30:25;0008;pbs\_mom;Job;_JOBID_.pbsserver;walltime _XX_ exceeded limit 60

---

<div class="post-metadata">

**Author:** ![arnaud](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@arnaud](https://community.openpbs.org/u/arnaud)\
**Post date:** [February 26, 2018, 8:53am UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877/3 "2018-02-26T08:53:11Z")

</div>

Like I said, this is not what happens on the cluster on which I am running the job, because of the specific PBS configuration on that cluster.

---

<div class="post-metadata">

**Author:** ![vchlum](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/vchlum/32/93_2.png) [@vchlum](https://community.openpbs.org/u/vchlum)\
**Post date:** [February 26, 2018, 10:07am UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877/4 "2018-02-26T10:07:01Z")

</div>

Assuming the cluster runs Linux, I would suggest checking whether ‘timeout’ command is available on the cluster.

With ‘timeout’ you could use something like:

```auto
timeout -s TERM 55 ./program.o

```

Without ‘timeout’ you could use something like:

```auto
./program.o &
PID=$!
sleep 55
kill -TERM $PID

```

Vasek

---

<div class="post-metadata">

**Author:** ![arnaud](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@arnaud](https://community.openpbs.org/u/arnaud)\
**Post date:** [March 3, 2018, 1:13pm UTC](https://community.openpbs.org/t/script-to-force-job-termination-after-a-given-time/877/5 "2018-03-03T13:13:18Z")

</div>

@vchlum: This works, thank you.
