0

I have been trying to open a terminal in GUI via crontab. This question describes it can't be done as desktop and crontab runs separately. Is there a way where I can trigger script from crontab but later attach that parent process to the display ?

I'm asking as I have multiple scripts that I need to run for few hours and would like to monitor them and give input in terminal when some event is triggered. I have tried exporting the DISPLAY=:0.0 variable and then run terminal from crontab but mostly it says something like Can't find the Display at :0.0.

I'm using the same user for crontab and for the desktop session.

Thanks

2 Answers 2

1

You need to have your desktop store its information about the display it's running on – when you log in, it needs to stash the values of necessary environment variables somewhere that your cron job will be able to read them.

(For example, through ~/.xprofile if that still works, or through a script run from ~/.config/autostart/.)

The most important environment variables are:

DISPLAY
XAUTHORITY
XDG_RUNTIME_DIR
DBUS_SESSION_BUS_ADDRESS

There are others which may influence the behavior of desktop apps. So with Ubuntu, the easiest way might be to use systemd to achieve this – many desktop components in fact already run much like cron jobs do, not part of the main "desktop session" process tree but rather detached through a 'systemd --user' process, and the desktop environment already stores the envvars there.

You can use systemd-run to start programs through the user's systemd instance. That way, you only need the single XDG_RUNTIME_DIR to be set (and it's a static value so you can hardcode it in your crontab – unlike DISPLAY which is more dynamic) and the started process will have the same environment variables as your desktop apps.

XDG_RUNTIME_DIR=/run/user/YOURUID
@hourly systemd-run --user --quiet --collect xterm
1

calling GUI terminal directly from crontab is not recommended, because it goes against the purpose of cron.

however, for your goal alone, i would consider:

option 1: find the value of the $DISPLAY variable within your cron job and set it before calling the GUI terminal

option 2: if you need input, store the required information/variables in files, which you can later extract the value in your cron job script. you can use your script directory (e.g. <script_dir>/.tmp) or user login scripts (e.g. ~/.profile or ~/.bashrc)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .