Run a shell script in background in Unix at specific time daily

2

I have a shell script task.sh that is needed to be run in background in Unix at specific time daily.

I know nohup is useful to run the script in background. But worrying about how to schedule this script to run daily at specified time.

Note: I am not root user. I don't have permissions for crontab command.

Any help would be greatly appreciated.

learner1

Posted 2016-02-15T07:23:13.233

Reputation: 155

Answers

2

The correct way to do this is using Crontab - if you don't have access to your crontab you really should speak to your administrator - they probably don't want you to be running scheduled jobs. That said, before you say to don't have permissions for the crontab command, try run "crontab -e" - each user has their own cron file, and crontab - will attempt to edit yours. (I note when I run crontab by itself as a user it just freezes)

You can make pretty much any job run in the background by appending an "&" character to the job. I would urge against this, but you could write a script in a while loop which checks the time, then waits a minute, checks again etc, and if it matches executes the command. You could run this script with & to run it in the background. You could also get into trouble for subverting the system....

Another command to look up is "screen". This allows you to attach and detach your terminal from the console - it can be a useful way of running something in the background, but being able to see and interact with it (You can screen, then execute the program in the foreground, then detach from screen, and reattach later on)

EDIT

A script to check the time once a minute and run the command at the appropriate time:

#! /bin/bash

RUNAT="07:54"

while [ 1 ]
do
    DATE=`/bin/date +%H:%M`
    if [ $DATE. = $RUNAT. ]
    then
        /path/to/script.sh
    fi

    sleep 60
done

Save this as (for example) mycron.sh, chmod 755 mycron.sh so it executes, Modify the RUNAT line to trigger when you want your script to run, then execute this script as mycron.sh & so it runs in the background.

This script will not cause any stacks to overflow because it only executes the command once per day, it does not "chain itself"

davidgo

Posted 2016-02-15T07:23:13.233

Reputation: 49 152

crontab -e is also not permitted. As you suggested to write a script using while loop, will that not cause stack to overflow ? and you mentioned "waits a minute", it means to use sleep ? – learner1 – 2016-02-15T09:48:39.693

I've modified my answer with an appropriate script and usage instructions - yes, it uses sleep, no it won't cause the stack to overflow. – davidgo – 2016-02-15T18:59:17.490

Thanks a lot @davidgo . Script is working damn fine. But I'm worried about resources (memory, CPU) utilization. If I never gonna kill the job, will there be any adverse effect on resources ? – learner1 – 2016-02-16T03:06:23.320

Excluding the script you run - which I can't comment on - the resources utilised by the script are negligible on any modern PC or server, and would probably not be discernible (save for its appearance in process lists etc - its not stealthy), let alone of any consequence. – davidgo – 2016-02-16T03:37:23.803

1

Provided that you have access to at (not likely if cron is not availably but worth a try) you can emulate simple cron jobs. The simplest way to do so is make at run the script at a given time, and let the script make at run itself again when it must run.

In my example, the script only writes the timestamp of its last run and does the timing of one minute:

/tmp$ cat cronbyat.sh
#!/bin/sh


echo "last run: `date +%Y%m%d-%H%M%S`" >> /tmp/cronbyat.log

echo "/tmp/cronbyat.sh" | at now + 1 minute

/tmp$ echo "/tmp/cronbyat.sh" | at now + 1 minute
warning: commands will be executed using /bin/sh
job 5 at Mon Feb 15 09:00:00 2016
/tmp$ tail -f /tmp/cronbyat.log
last run: 20160215-090000
last run: 20160215-090100
last run: 20160215-090200
last run: 20160215-090301
last run: 20160215-090400
^C
/tmp$ atq
10      Mon Feb 15 09:05:00 2016 a lx
/tmp$ atrm 10
/tmp$ atq
/tmp$

When the script is not needed any more, its current at job must be killed as shown in the last rows. Unfortunately, if a run is missed due to any cause, the chain is broken. Thus, some kind of feedback should be given and checked.

Gombai Sándor

Posted 2016-02-15T07:23:13.233

Reputation: 3 325

Thanks for the logic @Gombai. Sorry to say at command is also not permitted to use. But I think I got a clue from the logic. – learner1 – 2016-02-15T09:33:59.237