user2540748
user2540748

Reputation:

Cron ran only once

I'm running Ubuntu x64 14.04 and have a cron setup to run a shell script.

0 0 * * * sh root/delete.sh

It should run once a day at midnight. According to my logs, it ran once and then never ran again the next night.

Am I missing something really obvious?

Upvotes: 0

Views: 1795

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263247

It's hard to tell exactly what you're trying to do, but I can tell you what that command will actually (try to) do.

0 0 * * * sh root/delete.sh

Cron jobs run with the working directory set to the user's home directory. This command will run sh (which resolves to /bin/sh) passing it the string root/delete.sh as an argument. /bin/sh will interpret that as a file name; since it doesn't start with a /, it's interpreted relative to the current directory.

So if you have an executable script in $HOME/root/delete.sh, that line should execute it every night at midnight.

For clarity, you should probably (a) use an absolute pathname, and (b) make sure the script itself has a proper #! line (#!/bin/sh or #!/bin/bash), and invoke the script directly rather than passing its name to the sh command. Neither of these is necessary, but they'll make your intent move obvious.

If delete.sh is in the /root directory, not under your home directory, then you should have:

0 0 * * * /root/delete.sh

If it's under $HOME/root, then you should have:

0 0 * * * $HOME/root/delete.sh

Again, this depends on delete.sh being executable (chmod +x delete.sh) and having a proper #! line at the top.

Upvotes: 1

Related Questions