0

I created a simple crontab job.

It should execute every 15 minutes to alert me that I need to take a short break. However, it does not work. I don't know why.

Please help me if you have experience with crontab. I used the command crontab -e to create this file and save it.

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
*/15 * * * * /home/galaxy/Documents/projects/Bash/cron_job.sh

This file is supposed to execute the script cron_job.sh every 15 minutes.

This is a Bash shell script that has a chmod value of 500. It contains one command to create a new gnome-terminal and immediately run a command inside that new terminal to cat the file take_a_break. That is a text file with a simple message telling me to take a break.

Running from the terminal directly either the shell script or just running the command contained inside of it works flawlessly, but the crontab job does not work at all. Maybe I formatted something incorrectly.

Here is the contents of the file cron_job.sh:

 #!/bin/bash

gnome-terminal -e "bash -c \"cat /home/galaxy/Documents/ascii/take_a_break; exec bash\""

The reason I put the command inside a shell script is because cron did not run it previously, but cron also does not run the shell script.

I checked and it seems to me that I have the correct syntax.

Also if it is relevant, trying to run the crond command gives an error:

$ crond
No command 'crond' found, did you mean:
 Command 'cron' from package 'cron' (main)
crond: command not found
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Galaxy
  • 505
  • There are a few things that should be clarified: 1) Are you sure your job does not run? It seems very likely that the problem is, as always, a path issue. Putting the full path for your gnome-terminal might solve it. 2) Can you run the command /home/galaxy/Documents/projects/Bash/cron_job.sh on its own and does it do what you want? – Julie Pelletier Mar 16 '17 at 02:51
  • Note that it is useless to include the example crontab file with the syntax here. Your actual command is sufficient. Also note that troubleshooting involves simplifying everything and therefore testing something like date > /tmp/lastbreak as the job would allow you to see very easily if it actually runs or not. – Julie Pelletier Mar 16 '17 at 02:54
  • What do you mean by "putting the full path of the gnome-terminal"? And also yes, the command does work on its own in the command line as intended. – Galaxy Mar 16 '17 at 02:56
  • 2
    Running GUI applications (such as gnome-terminal) via cron often fails because cron's environment doesn't include the necessary DISPLAY variable. – steeldriver Mar 16 '17 at 03:04
  • What do you mean "it is useless to include the example crontab file with the syntax here"? I used the command crontab -e to edit the crontab file. I put my command after the comments lines. This is what my crontab file actually looks like when I run the command crontab -l. – Galaxy Mar 16 '17 at 03:06
  • type gnome-terminal will show you the full path. steeldriver is right about other environment variables that will need to be set. – Julie Pelletier Mar 16 '17 at 03:12
  • My point about the sample cron file is that it doesn't belong in this question and you should just show your command. The commented out syntax is not needed or wanted here. – Julie Pelletier Mar 16 '17 at 03:12

1 Answers1

2

So I found a solution to the problem:

I used the full path of the gnome-terminal and added the line export DISPLAY=:0.0 to the shell script.

Now my cron job works as intended.

#!/bin/bash

export DISPLAY=:0.0
/usr/bin/gnome-terminal -e "bash -c \"cat /home/galaxy/Documents/ascii/take_a_break; exec bash\""
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Galaxy
  • 505