user9671653
user9671653

Reputation:

How to make Crontab run a Python script that opens the Terminal and does stuff?

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

Upvotes: 0

Views: 4606

Answers (4)

mdkb
mdkb

Reputation: 402

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'

Upvotes: 2

Josh Correia
Josh Correia

Reputation: 4354

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'

Upvotes: 1

user9671653
user9671653

Reputation:

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.

Upvotes: 2

ferrix
ferrix

Reputation: 761

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.

Upvotes: 2

Related Questions