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 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
  • 7,426