0

I have a Pi 3B+ with Pi OS (PRETTY_NAME="Raspbian GNU/Linux 10 (buster)")

I want to run a script at a certain time, using at command.

For testing purposes i have made a test1.sh, to open Chromium browser using pyautogui.

This file is located in /home/pi/test1.sh and contains the following:

#!/usr/bin/python3
import pyautogui
pyautogui.click(67, 20) #click Chromium icon

This .sh executes fine when clicked in the GUI and also when I run it typing ./test1.sh in the Terminal.

Trying to use at command does not work and yields the following:

pi@raspberrypi:~ $ at 11:50
warning: commands will be executed using /bin/sh
at> ./test1.sh
at> < EOT> (Here is where I type ctrl+D)
job 11 at Fri Nov  5 11:50:00 2021

At the specified time, nothing happens. I know it must be something I am doing wrong, but for the life of me I can not figure out what it is. Help?

Edit:

the first / in the shebang was missing in the post, but not in the file.

atq returns nothing

ps ax returns text below (cut for brevity)

 PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:05 /sbin/init splash
    2 ?        S      0:00 [kthreadd]
    3 ?        I<     0:00 [rcu_gp]

date returns accurate date and time

cat /var/mail/pi returns the following:

Subject: Output from your job       11
To: pi@raspberrypi
Message-Id: <E1miwnI-0000tR-83@raspberrypi>
From: pi@raspberrypi
Date: Fri, 05 Nov 2021 11:50:00 +0100

sh: 49: ./test1.sh: not found

From pi@raspberrypi Fri Nov 05 12:05:00 2021
Return-path: <pi@raspberrypi>
Envelope-to: pi@raspberrypi
Delivery-date: Fri, 05 Nov 2021 12:05:00 +0100
Received: from pi by raspberrypi with local (Exim 4.92)
    (envelope-from <pi@raspberrypi>)
    id 1mix1o-0000xA-KG
    for pi@raspberrypi; Fri, 05 Nov 2021 12:05:00 +0100

This suggests that the file is not there, but when I type ls it is clearly shown.

Using the full path /home/pi/test1.sh also returns nothing but the mail received states the following:

Subject: Output from your job       13
To: pi@raspberrypi
Message-Id: <E1mjHmv-0000NT-4B@raspberrypi>
From: pi@raspberrypi
Date: Sat, 06 Nov 2021 10:15:01 +0100

Traceback (most recent call last):
  File "/home/pi/test1.sh", line 3, in <module>
    import pyautogui
  File "/home/pi/.local/lib/python3.7/site-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/home/pi/.local/lib/python3.7/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/lib/python3.7/os.py", line 678, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'
zuputoddu
  • 11
  • 3
  • Missing the first slash in `#!usr/bin/python3` (-> `#!/usr/bin/python3`)? – linuxfan says Reinstate Monica Nov 05 '21 at 11:19
  • The job you submit to at(1) refers to the current directory ("./test..."). But when the job will be launched, what will be the current directory? Try to write down the full path. – linuxfan says Reinstate Monica Nov 05 '21 at 11:21
  • Check if you received any mail following `at` job : `cat /var/mail/pi` – Philippe Nov 05 '21 at 11:56
  • Is your time zone set correctly? Does `date` print out the correct local time? – Nate Eldredge Nov 05 '21 at 15:50
  • 1
    @linuxfansaysReinstateMonica: `at` will execute the job with the same working directory that `at` was invoked in. This is documented behavior. So if `./test1.sh` works from a given directory, then running `at` in that same directory and typing `./test1.sh` should also work. – Nate Eldredge Nov 05 '21 at 15:52
  • Other checks: does `atq` show the job as you expect? Does `ps ax` show an `atd` daemon running? – Nate Eldredge Nov 05 '21 at 15:53
  • Trying to use the full path of the file is a step forward, it seems. But now, it seems the DISPLAY variable is not set. You can try to set it in code, but I suspect that some authorization error is likely to come. If it comes, a quick (and probably wrong) way is to execute "xhost +" to relaxe authorization. Or to do "su someuser" beforehand. – linuxfan says Reinstate Monica Nov 07 '21 at 07:09
  • Note to my previous comment: the documentation of at(1) says that userid and current directory are retained... from your analisys (received mails) it seems not. Anyway the DISPLAY variable is not remembered (that is stated in the doc. But at this point I would not trust it anymore). – linuxfan says Reinstate Monica Nov 07 '21 at 07:17
  • @linuxfansaysReinstateMonica: thank you for your reply. I will research into setting the DISPLAY variable. I will have to google that, as it is something that surpasses my current skill level. It might take a while to get back at you. Any hints are welcome. – zuputoddu Nov 07 '21 at 08:18

1 Answers1

1

As pointed out by @linuxfan in the comments:

The DISPLAY variable is not set.

problem was fixed by doing the following:

pi@raspberrypi:~ $ at now + 1 minute
warning: commands will be executed using /bin/sh
at> export DISPLAY=:0.0
at> /home/pi/test1.sh
at> <EOT>
job 17 at Sun Nov  7 10:55:00 2021
zuputoddu
  • 11
  • 3