LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2019, 07:46 PM   #1
CharlieHotel
LQ Newbie
 
Registered: Apr 2019
Posts: 7

Rep: Reputation: Disabled
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!
 
Old 11-02-2019, 07:57 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 15,864
Blog Entries: 27

Rep: Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662Reputation: 4662
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.
 
Old 11-02-2019, 08:15 PM   #3
CharlieHotel
LQ Newbie
 
Registered: Apr 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-02-2019, 09:11 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,349

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Quote:
Originally Posted by CharlieHotel View Post
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.

Last edited by scasey; 11-02-2019 at 09:15 PM.
 
1 members found this post helpful.
Old 11-03-2019, 04:47 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,390
Blog Entries: 9

Rep: Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652
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?
 
Old 11-03-2019, 09:22 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,512

Rep: Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133
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?
 
Old 11-04-2019, 02:29 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,390
Blog Entries: 9

Rep: Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652
Quote:
Originally Posted by michaelk View Post
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.
 
Old 11-04-2019, 02:38 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,575

Rep: Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340Reputation: 4340
Quote:
Originally Posted by CharlieHotel View Post
/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.
 
Old 11-04-2019, 02:55 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,284

Rep: Reputation: 588Reputation: 588Reputation: 588Reputation: 588Reputation: 588Reputation: 588
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).
 
Old 11-04-2019, 03:38 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,512

Rep: Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133
Quote:
Originally Posted by ondoho View Post
It clearly says ... (root) CMD ... in post #1.
Your right... root does have it's own user crontab.
 
Old 11-06-2019, 08:46 AM   #11
CharlieHotel
LQ Newbie
 
Registered: Apr 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.

Last edited by CharlieHotel; 11-06-2019 at 08:47 AM. Reason: Not completed
 
Old 11-06-2019, 09:14 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,512

Rep: Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133
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.
 
1 members found this post helpful.
Old 11-06-2019, 10:47 AM   #13
CharlieHotel
LQ Newbie
 
Registered: Apr 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
It worked! Thank you everyone!

Now that I know how to run bash scripts on reboot I can get on with my project.
 
Old 11-06-2019, 12:43 PM   #14
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,724

Rep: Reputation: 210Reputation: 210Reputation: 210
Quote:
Originally Posted by pan64 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 05:22 AM
[SOLVED] crontab does not reread the crontab file upon modification of the latter. stf92 Slackware 4 08-01-2015 07:12 PM
[SOLVED] crontab -e not running @reboot script EODSteven Linux - Newbie 21 04-28-2014 04:35 PM
[SOLVED] Problem with crontab:: command not executed properly via crontab Ankush Seth Linux - Newbie 11 11-11-2013 07:25 AM
Bash script not running within crontab job WrightExposure Linux - General 3 01-23-2007 07:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration