0

I have a Script called "test.sh" in my Home Directory

I want to be able to run it in crontab.

I have added

* * * * * /home/tom/test.sh

to the crontab however it doesn't seem to be excecuting.

I can excecute ./test.sh fine normally in a Terminal Window.

Edit:

This is the content of my script "test.sh"

#!/bin/sh
gnome-terminal -- sh -c 'cd Server && ./start.sh'

It creates a new terminal window, changes directory to Server and runs another executable script.

I do not know if this has something to do with the script not working in Cron.

The error I get in /var/log/syslog is:

CRON[18694]: (tom) CMD (sh /home/tom/test.sh)
CRON[18693]: (CRON) info (No MTA installed, discarding output)
tdubz
  • 41
  • CRON stands for Command Run ON. So when do you want it to run or how often do you want it tor run; every minute, five minutes, hour, day, week or month? – WinEunuuchs2Unix Apr 18 '20 at 15:45
  • I just want to test that it works first. After I will set it to do it every midnight which I believe is 0 0 * * * – tdubz Apr 18 '20 at 15:50
  • did you add it in user crontab or global crontab? –  Apr 18 '20 at 15:57
  • 3
    Works-in-terminal-but-doesn't-work-in-cron is one of the most common new-user questions. Try the Search bar at the top of the page to see hundreds of similar queries. Look up how to use output redirection to see the error messages. Then you will know what is wrong. – user535733 Apr 18 '20 at 16:05
  • It would be helpful to see the contents of /home/tom/test.sh. Also results from journalctl | grep test.sh might provide clues to you. – WinEunuuchs2Unix Apr 18 '20 at 16:14
  • cron was never really intended for running desktop applications - if you insist on doing it, see How to start a GUI application from cron? – steeldriver Apr 18 '20 at 22:01
  • I have tried * * * * * su tom -c "DISPLAY=:0.0 sh /home/tom/test.sh" however still doesn't seem to be working @steeldriver – tdubz Apr 19 '20 at 09:00
  • @tdubz for some applications, setting DISPLAY is not sufficient, they need to make a connection to the desktop session's DBUS. See for example How to use notify-send with crontab?. FWIW I would recommend putting the relevant DISPLAY and DBUS_SESSION_BUS_ADDRESS commands inside your test.sh script. – steeldriver Apr 19 '20 at 11:56
  • Thank you very much @steeldriver - Cron is now executing the script after adding eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"; to my test.sh. – tdubz Apr 19 '20 at 12:12

1 Answers1

0

First and foremost, the syntax for the execution times is demonstrated here. The CronTab can be confusing, but there are an abundance of guides out there to help.

Now, in your case you have:

* * * * * /home/tom/test.sh

Depending on how you edited your Crontab, this could be a permission issue. Using bash, I always did the following:

* * * * * sh /home/tom/test.sh

Which is the same as the following:

* * * * * /home/tom/test.sh

With the theory that test.sh has a somewhat similar structure:

#!/bin/bash

for i in */ .*/ ; do 
    echo -n $i": " ; 
    (find "$i" -type f | wc -l) ; 
done

The last thing to check is that the script is executable:

chmod +x /home/tom/test.sh

For me, I performed the following on my Crontab:

# Example Crontab command:
# * * * * * /home/mn/Documents/Scripts/counta.sh >> /home/mn/Documents/Scripts/test

This was able to successfully run and output the test file as required.

  • @user535733 yeah, I was wrong so I went ahead and attempted to correct myself, but you commented too fast to see the correction ;) – DankyNanky Apr 18 '20 at 16:03
  • 2
    should remove the dot from ./home/tom/test.sh –  Apr 18 '20 at 16:05
  • steeldriver you are slow today :)= –  Apr 18 '20 at 16:06
  • Guys, I give up on syntax. I'm done. More damage than good! – DankyNanky Apr 18 '20 at 16:10
  • @WinEunuuchs2Unix you mean answer, right? Ehhh, if I've fumbled again on this one I'll delete it. But thanks to the other two for the pickups ;) – DankyNanky Apr 18 '20 at 16:15
  • Thanks for the answer @DankyNanky - It seems as though the script still does not execute properly despite using "sh" before the command. The script is definitely executable because I can run it just fine in a Terminal Window. I have edited my original question with more background information about the script I would like to run and the error I am getting. I hope that proves to be helpful. – tdubz Apr 18 '20 at 17:55
  • https://askubuntu.com/q/222512/212930 – DankyNanky Apr 18 '20 at 17:58