How to Run a Script At a Certain Time on Linux

How to run a script at a certain time on Linux?

Look at the following:

echo "ls -l" | at 07:00

This code line executes "ls -l" at a specific time. This is an example of executing something (a command in my example) at a specific time. "at" is the command you were really looking for. You can read the specifications here:

http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html
http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html

Hope it helps!

Execute a shell script everyday at specific time

You want to edit your crontab file using

crontab -e

Then you want to add

55 23 * * * COMMAND TO BE EXECUTED

for more info look at this

Run a command at a specific time

You could try this:

at 1843 <<_EOF_
php /run/this/script.php
_EOF_

edit if what you want to do is run Firefox, try this:

at 1843 <<_EOF_
DISPLAY=:0.0 /usr/bin/firefox
_EOF_

How to run a scrip if the time is matched with a time in another time zones

As you already know how to set the at command to execute a command
at the specified time, and how to convert the EST to the local time,
you can just combine them:

echo "python3 myfile.py" | at $(date -d "19:30 EST" +%R)

When you invoke the at command, it always warns
"commands will be executed using /bin/sh". It will matter only if we invoke
a bash specific command such as:

echo "shopt -s globstar; ls **" | at ..

which will fail.

In our case, the command python3 myfile.py will run with both
/bin/sh and /bin/bash then you do not worry about the warning.

date -d STRING interprets the STRING as a date/time representation
and prints the converted date/time in the specified format +%R.

If you want to send the output to a file, you can say:

echo "python3 myfile.py > /path/to/a/file" | at $(date -d "19:30 EST" +%R)

In order to output to the current terminal, first identify the terminal
with tty command:

$ tty
=> /dev/pts/0

Then redirect the output to the terminal with:

echo "python3 myfile.py > /dev/pts/0" | at $(date -d "19:30 EST" +%R)

Running a shell script for a certain duration

A few issues:

  • The conditional construct is while (( ... )); do and not while $(( ... )); do
  • The colon at the end of your line

    while $((curr_time < $((start_time + run_end_time)) )) :

    must not be there.

  • You're assigning the string SECONDS when you want the value of the variable; the assignments should look like var=$SECONDS instead of var=SECONDS.

Complete script, with a few suggestions and my idea of indentation:

#!/bin/bash

some_work () {
echo "Working for $1 minutes"
if (( $1 > 0 )); then
echo "Setting timer for $1 minutes"
run_end_time=$(($1 * 60))
start_time=$SECONDS
curr_time=$start_time # Want this to be the same value
while ((curr_time < $((start_time + run_end_time)) )); do
#Do some work here
curr_time=$SECONDS
done
fi
}

sleep_time () {
echo "Sleeping for $1 minutes"
sleep $(($1 * 60))
}

if (( $# > 1 )); then
echo "Starting Steeplechase run for $1/$2"
while true; do
some_work $1
sleep_time $2
done
fi


Related Topics



Leave a reply



Submit