1

Currently, I am using cloud VMs to run my code and because of that I am assigned with a new VM that is in a different time zone. I want to run a bash script that runs a python script at 7:30 pm (Eastern time). From here I know how to run a bash script at a specific time, e.g., echo "ls -l" | at 07:00. From here I know how to get the current time of Eastern time, e.g., TZ=America/New_York date. Also, from here I know how to get only the time using date +%R.

I am a Python coder and tried my best to write a sudo code that shows what I am trying to accomplish as a bash script:

while true
do
Now=TZ=America/New_York date +%R
if [Now -eq 7:30pm] 
then
python3 myfile.py
done
Saeed
  • 598
  • 10
  • 19
  • How about `echo "python3 myfile.py" | at $(date -d "19:30 EST" +%R)`? – tshiono Feb 22 '22 at 03:15
  • @tshiono: I get `warning: commands will be executed using /bin/sh`, what is the meaning of `executing using /bin/sh`? Also, what does `date -d` so here, especially flag `-d`? Finally, it looks like it was executed but it did not printed out `"python3 myfile.py"` in a terminal where I ran by bash file, how can I see the executed log? – Saeed Feb 22 '22 at 03:25
  • The warning is a routine phrase of `at` command just telling the input commands are *not* executed under the user's favorite shell such as /bin/bash. There is no difference so far and you can ignore it. `date -d STRING` takes the provided STRING as a time instead of *now*. In my example above it converts `19:30 EST` to the local time. As for the output of `at` command, please take a look of [this discussion](https://stackoverflow.com/questions/10873417/how-to-get-the-output-of-at-command-in-current-or-another-terminal-window). – tshiono Feb 22 '22 at 04:00
  • @tshiono: "are not executed under the user's favorite shell such as /bin/bash." but the warning says "will be executed using /bin/sh". I did not get it. Also, please post your solution as an answer so that I can accept it. – Saeed Feb 22 '22 at 04:44
  • Okay, I've posted my answer adding some explanations. Hope it explains better. BR. – tshiono Feb 22 '22 at 06:34

1 Answers1

1

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)
tshiono
  • 21,248
  • 2
  • 14
  • 22