0

I want Crontab to run Python script at specific time. That Python script opens the Terminal with "subprocess" library and writes stuff with "Pyautogui" library. How to do that?

I studied Crontab a little bit and made it work with my Bash script, which does something else. I learned that Crontab has some kind of limitations. I used DBUS_SESSION_BUS_ADDRESS to enable gsettings but now, it's about opening a Terminal and writing stuff.

Crontab entry:00 15 * * * python3 /home/Admin/Desktop/shutdown_script.py

How to do that?

Goal: My goal is to shutdown my computer and other 19 computers at the same time at specific time, when I leave the place. I use CSSH to connect to other computers. My script does that and writes password and sudo shutdown now to shut them down, all, at the same time.

EDIT: Python script

#!/usr/bin/python3

import pyautogui, time

pyautogui.hotkey("ctrlleft", "altleft", "t")
time.sleep(1)
pyautogui.write("Hello World!", interval=0.05)

Manually running it in Terminal = works; Trying to do that with Crontab = not working

BUMP: If it's impossible or no one knows, how to do that or has any ideas then I'll look for another way, maybe schedule stuff in Python with some Python library or.. idk.. find something (idea), that might accomplish this :D

  • what’s your goal? you might not need to open the terminal – gold_cy Feb 21 '19 at 11:30
  • My goal is to shutdown my computer and other 19 computers at the same time at specific time, when I leave the place. –  Feb 21 '19 at 11:35
  • In the general case, there is no guarantee that there is a graphical session when the job runs, thus no way to know if you can run Terminal, and if so in whose session it should run, I'm almost certain this is overcomplicating things, unless you specifically need to interact with logged-in users and their environments, in which case we need to know much more about the precise circumstances. – tripleee Feb 21 '19 at 11:44
  • Also, which Terminal and how to run it and what it requires and what you can do with it depends a lot on the environment. Terminal sounds like MacOS but I guess the other stuff here sounds vaguely like Ubuntu ...? – tripleee Feb 21 '19 at 11:47
  • Yeah, I meant it in Ubuntu :D. Pyautogui opens new Terminal (Bash, checked with echo $0, echo $SHELL) if Bash process isn't found by grep command. I really need to interact with logged-in users and their environments. I'll give a small example of my script, hang on! –  Feb 21 '19 at 11:49

4 Answers4

2

With Xorg, there are two things you need to be able to poke into a running window session and start running applications:

  1. Display configuration
  2. XAUTH

The display bit is easy:

export DISPLAY=:0

The format of that environment variable is practically hostname:displaynum.screennum which abbreviates to what you saw.

Xauth is a way to pass a secret in the X session so that the server knows that this window belongs to this user. Dirty way:

export XAUTHORITY=/home/$your_username/.Xauthority

Cleaner and more robust way:

xauth add $(xauth list $DISPLAY)

That passes the output of xauth list as a parameter to xauth add

Then run your terminal from command line.

ferrix
  • 763
  • 3
  • 14
  • This is a good guide to show a beginner, how to set up a DISPLAY environment but.. I knew this already. And it doesn't answer to the question at all. I already tried DISPLAY thing before I made this post. No results and still looking for a solution. :( –  Feb 21 '19 at 20:06
  • In my experience, `DISPLAY` and `XAUTHORITY` _both_ is all you need. Since you want a terminal window, there is something like `xterm -e &` to do the actual execution. That would get rid of the hotkey part. Then again. I don't know if it is any different to run something through cron in a bash shell on a terminal or the same thing without the terminal. You did have some other actions you wanted to perform on X in an earlier version, so that may well be valid. – ferrix Feb 21 '19 at 23:12
2

Got it.

04 11 * * * export DISPLAY=:0 && /usr/bin/python3 /home/Admin/Desktop/shutdown_script.py

Putting export DISPLAY=:0 to Crontab made it work.

Place that I found and it helped: Click me

It runs this script at 11:04 AM everyday.

  • I didn't mention this but I did this in Linux. Ubuntu 16.04. Below my answer is a solution to Mac version. I hope this post helps you :). Have a nice day. –  Aug 02 '19 at 14:31
2

I found this did not work to open a terminal display but only ran my python script in the background (visible only in system processes).

It maybe because I am on linux Lubuntu 17.10 but I found other people seeing the same issue so thought I would share the solution that worked for me here.

the below was the code I added into in crontab to make it work in terminal window (lxterminal in my case) you could also put $SHELL after .py if you needed it to keep the terminal window open after the python script terminates.

export DISPLAY=:0 && lxterminal -e bash -c '/fullpath/mypythonscript.py'
mdkb
  • 372
  • 1
  • 14
1

If you're using a Mac, you can use AppleScripts combined with cron in order to emulate opening a Terminal and running the script from within the Terminal.

0 15 * * * osascript -e 'tell app "Terminal" to do script "python3 /home/Admin/Desktop/shutdown_script.py" activate'

Josh Correia
  • 3,807
  • 3
  • 33
  • 50