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 notwhile $(( ... )); 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 likevar=$SECONDS
instead ofvar=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
How to Parse Xml Using Shellscript
Docker Can't Connect to Docker Daemon
How to Join Multiple Lines of File Names into One With Custom Delimiter
How to Run Script as Another User Without Password
Command Line Program to Create Website Screenshots (On Linux)
How to Use Sed to Change My Configuration Files, With Flexible Keys and Values
Cronjob Does Not Execute a Script That Works Fine Standalone
How to Print the Ld(Linker) Search Path
Use Sudo With Password as Parameter
Linking Openssl Libraries to a Program
Why Do X86-64 Linux System Calls Modify Rcx, and What Does the Value Mean
Tool to Trace Local Function Calls in Linux
How to Find All Serial Devices (Ttys, Ttyusb, ..) on Linux Without Opening Them
How to Create a File With a Given Size in Linux
Ipc Performance: Named Pipe VS Socket
Linux Bash: Multiple Variable Assignment
How to Run Multiple Background Commands in Bash in a Single Line
How to Measure the Actual Memory Usage of an Application or Process