Problem running crontab every two minutes using the root user

6

I have a very simple .sh file:

echo "\nhola" >> /etc/holaadd.txt

I just run it and add a line ... my problem is that I want to add it to: /etc/crontab directly and I added it in the following way:

 */2 * * * * root /bin/hola.sh

but the two minutes pass and it does not run I verify the file holaadd.txt and nothing is added.

    
asked by Eder Ditmar Rocca Alvarez 23.09.2016 в 00:22
source

2 answers

4

Verify crontab file

The file /etc/crontab has to end with a blank line. If at the end of the file there is no blank line (ie empty), Mr. Cron refuses to work.

Example:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the 'crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
*/2 *    * * * root /bin/hola.sh
#<--- Esto es una linea en blanco

Check that crontab is started:

Then there is the option that the service is not started, to check that the cron is started first put: pgrep cron , if you do not get an answer execute service cron start (with root permissions, or failing sudo)

Summon the program with the absolute path:

Probably your problem is that you 'assume' that your program will be executed with sh, but you have to keep in mind that cron does not always know what to use to execute the command. That's why modify the order changing:

 */2 *    * * * root /bin/hola.sh

for

*/2 *    * * * root /bin/sh /bin/hola.sh

* assuming that /bin/hola.sh is the path of your program and that /bin/sh is the location of sh. This last value can be obtained by executing the which sh command.

    
answered by 14.07.2017 в 15:40
1

It is not recommended to edit /etc/crontab directly.

Cron tables must be edited using the crontab -e

command

A typical problem with this is that vi is the default editor and not everyone knows how to use it. You can change it to the one you like most with the environment variable EDITOR . For example:

export EDITOR=nano
crontab -e

Using crontab -e you do not run the risk that /etc/crontab is rewritten without you noticing and it will also warn you if you have made an error in the lines you have entered. It also allows non-root users to edit their own table.

    
answered by 23.09.2016 в 08:53