Why isnt my cron job working?

November 16, 2016 5.4k views
Linux Commands Ubuntu 16.04

For some reason my cron entry isn't running at all. I'm trying to make a job that executes ZNC after reboots and crashes.

The commands I tried:

*/10 * * * * /usr/local/bin/znc >/dev/null 2>&1

Also tried adding the user:

*/10 * * * * znc-admin /usr/local/bin/znc >/dev/null 2>&1

Neither one is working. These were added using the "crontab -e" command, under root user.

These were tested by rebooting and waiting 10 minutes for the command to work.

3 Answers

The problem with the cronjob is that it has one extra star (which causes a syntax error). Replace it with this one:

*/10 * * * /usr/local/bin/znc >/dev/null 2>&1

Sorry, but I don't agree @ioanmoldovan199, you have enough stars.

*/10 * * * * part is OK.
General syntax is OK - * * * * * * where from left to right * means:
1 - Minute (0-59)
2 - Hour (0-23)
3 - Day of Month (1-31)
4 - Month (0-7)
5 - Day of Week (0-6)
6 - Command
So, your syntax means execute command every 10 minutes. */10 on minute part, means it'll be repetitive - it'll execute every 10 minutes.

Even if you look at ZNC wiki, you'll see that same line:

*/10 * * * * /usr/local/bin/znc >/dev/null 2>&1

My guess is that command is invalid.
What could be a problem? Well, maybe ZNC is not located at /usr/local/bin/znc.
Verify location of it using which command. which will return you where it is stored. Execute this:

  • which znc
Sample output of 'which'

If you installed using apt-get, you'll probably have it in /usr/bin/znc instead of /usr/local/bin/znc, so line for crontab would be:

*/10 * * * * /usr/bin/znc >/dev/null 2>&1

If output of which is something other, use it.

To verify is it problem, you can try executing location of it - /usr/local/bin/znc.
If you type this in terminal:

  • /usr/local/bin/znc

In case it is under that location you'll see something along:

Sample output
[ .. ] Checking for list of available modules... [ >> ] ok ...

(or any valid ZNC output).
Well, if it isn't under that location, you'll see this:

Sample output
-bash: /usr/local/bin/znc: No such file or directory
  • I'll give this a try.
    Is putting "znc-admin" after the stars to run it as that user a valid command as well?

    edit: the "which" command returns /usr/local/bin/znc

    • I saw your edit, maybe it is not working because you're running it as root. If you try to run it as root, following will arise:

      • [ .. ] Checking for list of available modules...
      • [ >> ] ok
      • [ ** ] You are running ZNC as root! Don't do that! There are not many valid
      • [ ** ] reasons for this and it can, in theory, cause great damage!

      You can't put user with crontab -e or sudo crontab -e.
      crontab -e will let you change cronjobs for current user.
      sudo crontab -e will let you change cronjobs for root user.

      You can one of these:

      1. Use sudo crontab -u znc-admin -e. With this, you should be editing cronjobs for znc-admin user. There, at the end add same line as you would: */10 * * * * /usr/local/bin/znc >/dev/null 2>&1 If this is not working for you - you can login as that user. Make sure you only follow this part or above part with sudo crontab -u znc-admin -e. For example on your root user you can execute: command su - znc-admin That will login you to znc-admin user. Execute crontab -e and add needed line (same as above).

      Now there is second possibility. That's to use /etc/crontab file. In that file you can supply user for command to be executed.
      Open that file with your favorite text editor:

      • sudo nano /etc/crontab

      At the end add following line:

      */10 * * * * znc-admin /usr/local/bin/znc >/dev/null 2>&1

      Save it and exit editor.

      Optional, to be sure it'll affect changes, you can restart cron service:

      • sudo systemctl restart cron

      I hope it'll work for you. :)

      • Okay. I finally got it, turns out that I had to add the cron job to the users crontab using:

        crontab -u znc-admin -e

        Now to figure out how to replace the hair that fell out caused by the frustration.

        Thanks for your help as well!

Thank you for your answer. I had the same problem. Now it's okay.
If you also need help I work for this service and I can help you.

Have another answer? Share your knowledge.