0

I am trying to run a python script in a new terminal using crontab:

Crontab:

* * * * * /usr/bin/sh /root/teststart.sh

teststart.sh:

#!/bin/sh
xfce4-terminal -e "/usr/bin/python3 /root/teststart.py" --hold

teststart.py has a query and a subprocess.call to gnome-terminal:

subprocess.call(['gnome-terminal', '-e', 'python3 scriptwithvariable.py %s' % (inputvariable)])

When I run # sh teststart.sh everything works fine, but the crontab doesn't execute the scripts.

Scripter
  • 101
  • 1
    If you want this cron job to run when you boot your computer change * * * * * to @reboot, Also use sudo crontab -e instead of crontab -e which is your regular user ID. – WinEunuuchs2Unix Jun 14 '20 at 21:43

1 Answers1

0

The terminal is a window, which a cronjob doesn't support. So you could export the display in your dash and maybe even your XAuthority - but why?

Cron runs in a "headless" environment, so it doesn't know anything about how to open a window.

What you could to - if you need a terminal running - is to install tmux. Cron could create and execute a tmux session which you could access any time.

--EDIT --

I'm not sure, but tmux doesn't work this way. Below an example that works on any machine, that has systemd installed:

[Unit]
Description=Python Example

[Service] ExecStart=/usr/bin/python3 /root/bin/somePath/MyPythonProgram.py StandardOutput=journal

[Install] WantedBy=multi-user.target

But this is not interactive. You would have to enable and start the service once.

Another example with tmux (that I couldn't get started with systemd yet):

#!/bin/bash
SESSION=rec
#get the current directory
DIR="$(dirname "$0")"
tmux start-server
# if the session is already running, just attach to it.
tmux has-session -t $SESSION
if [ $? -eq 0 ]; then
       echo "Session $SESSION already exists. Attaching."
       sleep 1
       tmux attach -t $SESSION
       exit 0;
fi

create a new session, named $SESSION, and detach from it

tmux new-session -d -s $SESSION -n 'Daemon'

split into 2 panes vertically window 0

tmux split-window -v -t 0 tmux new-window -n 'Web Server' #select the only window ... tmux select-window -t 0 #issue the commands for pane 0 and 1 tmux send-keys -t 0 $DIR/startDaemon.sh C-m tmux send-keys -t 1 $DIR/startWebServer.sh C-m #make the top pane the selected one... tmux select-pane -t 0 #and attach the session tmux a

This runs two sessions in tmux.

kanehekili
  • 6,402
  • I changed the crontab to start.sh: #!/bin/bash tmux new-session ; send-keys "/usr/bin/python3 /root/teststart.py" Enter exit 0 This doesn't work either. – Scripter Jun 15 '20 at 19:16
  • Might be this causing it: # Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

    Use “-- ” to terminate the options and put the command line to execute after it.

    Error creating terminal: Failed to get screen from object path /org/gnome/Terminal/screen/d26ca3de_608a_45e0_9021_f0ac51ab2acc

    – Scripter Jun 15 '20 at 19:26
  • @Scripter: See my edited answer. Please note that there is no way to open a gnome terminal. tmux is the terminal! – kanehekili Jun 19 '20 at 18:32