linuxbashcronxserver

Cron Task which displays something (task launched but nothing happens)


I well used the command "crontab -e" and added this task :

* * * * * * bash /home/user1/launcher.sh

The content of launcher.sh is :

#!/bin/bash

PATH=/home/user1/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

/usr/bin/gedit

Permissions of launcher.sh are 777.

I was expecting to see gedit launched every minute....but nothing happens. Why ?

I think crontab well launch "launcher.sh", as i can see in syslog :

Feb  7 22:58:01 librempc CRON[5922]: (user1) CMD (bash /home/user1/launcher.sh)
Feb  7 22:58:01 librempc CRON[5921]: (CRON) info (No MTA installed, discarding output)

Solution

  • By default, Cron executes tasks with very few environnement variables.

    It was well explained there : cron & the environment variables

    For the environment variable "PATH", it's possible to define it in the bash script of the task itself :

    PATH=/home/user1/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    

    But for the environment variable "DISPLAY" (which is mandatory if the task you launch display something), it must be precised in cron file directly.

    * * * * * * DISPLAY=:0 bash /home/user1/launcher.sh