cron jobs

CptCharis

Well-Known Member
Credits
189
Hello everybody,
I am trying to set up an update and reboot cron job for my home server.

I use crontab -e and I enter

* * * * * root (apt-get update && apt-get -y -d upgrade; /sbin/shutdown -r now) > /dev/null
but command doesn't look to work
I can see in /var/log/syslog this output
Jun 1 17:05:01 LinuxMint CRON[5072]: (root) CMD (charis (sudo apt-get update && sudo apt-get -y upgrade; /sbin/shutdown -r now) > /dev/null)
Jun 1 17:05:01 LinuxMint CRON[5071]: (CRON) info (No MTA installed, discarding output)

I don't think that MTA is a problem since command is running

Later I change the cron job
* * * * * charis (apt-get update && apt-get -y -d upgrade; /sbin/shutdown -r now) > /home/charis/cronout.txt
With same results and no cronout.txt file was produced.

Can somebody help? What is wrong?
 


dos2unix

Well-Known Member
Credits
1,029
* * * * * ... Hmmm... wouldnt that be every minute of everyday?

&& this put the first process in the background and starts the second process while it is still running.
It can't do the upgrade, until the update is done. You probably want to use ; instead of &&
That way, it will wait until the first process is done. (Like you did with the shutdown)
MTA is just a mail agent that wants to email you the results of the cron job.

Finally I'm not certain that piping the output of all this to /dev/null is what you want to do.
 

CptCharis

Well-Known Member
Credits
189
* * * * * ... Hmmm... wouldnt that be every minute of everyday?
is just for test cron, of course it will be changed later

&& this put the first process in the background and starts the second process while it is still running.
is an "and" operator, means that the second command will not be executed if the first command failed

Finally I'm not certain that piping the output of all this to /dev/null is what you want to do.
it's used for a neat environment
 

dos2unix

Well-Known Member
Credits
1,029
is just for test cron, of course it will be changed later
If your updates takes more than a minute (which I'm pretty sure it will) a second instance will start while the first one is running.

is an "and" operator, means that the second command will not be executed if the first command failed
This is incorrect. The && will "concurrently" start the second process while the first process is still running.

There is an easy way to prove this if you have something like postgresql, tomcat or apache installed.

Do something like systemctl start httpd && tail -f /var/log/httpd/error_log

Does it wait until apache is completely started up before it starts showing you the logs, or does it show you the logs while apache is starting up?
 
Last edited:

CptCharis

Well-Known Member
Credits
189
Hello again, problem solved .

Just keep in your mind that when you are setting up cron jobs that need sudo privileges you have to open crontab -e with sudo or log in as root.
By the way I make cron job a little bit better.
since cron needs the full path of command.
* * * * (/use/bin/apt-get update && /use/bin/apt-get -y upgrade && /sbin/shutdown -r now) 2&>1 >> /home/charis/update.log
 


Members online


Top