LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab not running bash script on reboot (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-not-running-bash-script-on-reboot-4175663584/)

CharlieHotel 11-02-2019 06:46 PM

Crontab not running bash script on reboot
 
Hello.

I am trying to run a bash script whenever the computer reboots. I understand that @reboot <path_to_script> in the crontab will do this.

I put "@reboot /home/bin/wormy2" at the bottom line of my crontab.

/home/bin/wormy2 is my bash script. I know the bash script works and that it has executable permissions.

The errors I found in /var/log/syslog are
-------------------
Nov 2 17:30:30 mint cron[1421]: (CRON) INFO (pidfile fd = 3)
Nov 2 17:30:30 mint cron[1421]: (CRON) INFO (Running @reboot jobs)
Nov 2 17:30:30 mint CRON[1469]: (root) CMD (/home/bin/wormy2)
Nov 2 17:30:30 mint CRON[1464]: (CRON) info (No MTA installed, discarding output)
-------------------

I don't know what this means. Can someone help me know what I need to do to get my crontab working?

Thank you!

frankbell 11-02-2019 06:57 PM

Is the cronjob supposed the send an email?

I asked because

Quote:

(No MTA installed, discarding output)
"No Mail Transfer Agent installed." If one is installed, it may be misconfigured.

CharlieHotel 11-02-2019 07:15 PM

Quote:

Is the cronjob supposed the send an email?
No, it's not.

This is the bash script that it is trying to run:
------------------------
#!/bin/bash
date >> ~/Documents/.worm_logs/worm_logs.log
echo '==================================' >> ~/Documents/.worm_logs/worm_logs.log
echo 'Wormy Game 2' >> ~/Documents/.worm_logs/worm_logs.log
echo '==================================' >> ~/Documents/.worm_logs/worm_logs.log
python3 ~/Desktop/wormy2.py >> ~/Documents/.worm_logs/worm_logs.log 2>> ~/Documents/.worm_logs/worm_logs.log
nums='1 2 3 4'
for i in $nums
do
echo ' ' >> ~/Documents/.worm_logs/worm_logs.log
done
------------------------

The ~/Desktop/wormy2.py is a python program of the simple snake game.

scasey 11-02-2019 08:11 PM

Quote:

Originally Posted by CharlieHotel (Post 6053422)
No, it's not.

This is the bash script that it is trying to run:
------------------------
#!/bin/bash
date >> ~/Documents/.worm_logs/worm_logs.log
echo '==================================' >> ~/Documents/.worm_logs/worm_logs.log
echo 'Wormy Game 2' >> ~/Documents/.worm_logs/worm_logs.log
echo '==================================' >> ~/Documents/.worm_logs/worm_logs.log
python3 ~/Desktop/wormy2.py >> ~/Documents/.worm_logs/worm_logs.log 2>> ~/Documents/.worm_logs/worm_logs.log
nums='1 2 3 4'
for i in $nums
do
echo ' ' >> ~/Documents/.worm_logs/worm_logs.log
done
------------------------

The ~/Desktop/wormy2.py is a python program of the simple snake game.

Replace the twiddles in the script with the the actual absolute path. cron don’t know from that shortcut for $HOME.

PS please use code tags when posting code. See the link in my sig.

PPS The “no MTA” message is probably from cron. By default it’s usually configured to send an email on error.

ondoho 11-03-2019 03:47 AM

I think the MTA warning is unimportant.
How do you determine that "It does not work"?
There are a few potential snags in your script but nothing that should make it fail completely.
Of course, root's $HOME is not the same as your user's $HOME... so the log files will show up elsewhere...
Are you sure you want to run that game as root?

michaelk 11-03-2019 08:22 AM

A users cron job runs as that user. A system cron runs as the specified user which is typically root.

Does the script write to the log files? A cron job is not associated with a terminal window or the desktop which means the game will not eaisly run from cron. I assume the game is a window program?

Is autostarting the game when you login an option?

ondoho 11-04-2019 01:29 AM

Quote:

Originally Posted by michaelk (Post 6053600)
A users cron job runs as that user. A system cron runs as the specified user which is typically root.

It clearly says ... (root) CMD ... in post #1.

pan64 11-04-2019 01:38 AM

Quote:

Originally Posted by CharlieHotel (Post 6053418)
/home/bin/wormy2 is my bash script. I know the bash script works and that it has executable permissions.

This is most probably false. The script will get different environment when running from cron, therefore there can be problems.
The simple solution is to use full path everywhere (including python and date), do not use ~ at all).

Furthermore you can modify the cronjob entry like this:
Code:

@reboot /home/bin/wormy2 >/tmp/wormy2.log 2>&1
to catch some error messages.

MadeInGermany 11-04-2019 01:55 AM

A game? Without special tricks you cannot run a game from a crontab because it cannot connect to your "screen" (graphical display or tty/terminal).

michaelk 11-04-2019 02:38 AM

Quote:

Originally Posted by ondoho (Post 6053818)
It clearly says ... (root) CMD ... in post #1.

Your right... root does have it's own user crontab.

CharlieHotel 11-06-2019 07:46 AM

I'm sorry for the misunderstanding.

I wanted to try to run a simple bash script on reboot as a test, and since I already had wormy2 specified as a bash script and since I could run it from the terminal by typing 'Wormy2', I supposed that it would work in the crontab.

Running this game was not my end goal.

I wanted to run other bash scripts on reboot, and just wanted to test it with something.

I will try to program another bash script and see if it works.

PS. The game is a window game.
PPS. I will avoid '~'
PPPS. What I intend to do has to be run from the root crontab.

michaelk 11-06-2019 08:14 AM

As a simple test try
Code:

#!/bin/bash
/bin/date >> /root/onboot.txt

The script can run without needing a terminal window or the desktop. If it writes to file with the correct date/time it works.

CharlieHotel 11-06-2019 09:47 AM

It worked! Thank you everyone!

Now that I know how to run bash scripts on reboot I can get on with my project.

rnturn 11-06-2019 11:43 AM

Quote:

Originally Posted by pan64 (Post 6053823)
Furthermore you can modify the cronjob entry like this:
Code:

@reboot /home/bin/wormy2 >/tmp/wormy2.log 2>&1
to catch some error messages.

That last bit is probably why the MTA error is more important than suggested. Errors are normally emailed to the user.


All times are GMT -5. The time now is 11:51 PM.