1

Having issues making crontab run certain commands, despite the PATH and SHELL being set correctly.

Here is the env of the machine:

SHELL=/bin/bash
USER=ubuntu
MAIL=/var/mail/ubuntu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/gopath/bin

Here is the env of cron (looks the same):

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/gopath/bin
PWD=/home/ubuntu

Then, in the crontab:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/gopath/bin
SHELL=/bin/bash
*/1 * * * * "whoami"
*/1 * * * * "whoami && which whoami"

The first whoami task succeeds, but the second fails with:

/bin/bash: whoami && which whoami: command not found

because which is not found. However, this is quite strange as:

$ which whoami
/usr/bin/whoami

$ which which
/usr/bin/which

And /usr/bin is on the PATH in cron. What gives?

| improve this question | | | | |
  • Would you try the 2nd command in your sequence by explicitly designating the path of the executable? Also give me the output of: echo $path within the context of the whoami and which whoami commands. Full paths within CronTab commands are a generally accepted best practice. You can also try to set your path as follows: PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin It seems like your $PATH has a lot of $CRUD in it, sir. – Tyler Maginnis Sep 30 '14 at 17:16
  • */1 * * * * "echo $PATH" gives: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/gopath/bin" as expected. – Jake Sep 30 '14 at 17:19
  • So you are invoking BASH syntax within cron, which won't work. Other poster seconded that. If you are absolutely requiring to do this inside the crontab, instead of an external script, do invoke your commands via bash... So try $> bash -c 'whoami && which whoami' – Tyler Maginnis Sep 30 '14 at 17:20
  • Re:CRUD that's (mostly) the default $PATH of EC2 instances. – Jake Sep 30 '14 at 17:20
  • So encapsulate your crontab commands inside a bash -c command which exports a bash session... put whatever you need inside '' and you will be able to use operators. – Tyler Maginnis Sep 30 '14 at 17:22
2

You shouldn't quote the cron job.

You have

*/1 * * * * "whoami && which whoami"

Which is looking a command literally called whoami && which whoami. Such as /usr/bin/whoami && which whoami. Obviously, this command does not exist. Remove the quotes so that the command is properly interpreted:

*/1 * * * * whoami && which whoami
| improve this answer | | | | |
0

Generally speaking, if you want to run multiple commands in a single cron task, you really should put them in a script.

You shouldn't be placing double-quotes around your statement like that. It's likely to be treated as a literal. If you are truly intent on running them inline, you can do something like the following: bash -c 'whoami && which whoami'

But again, at that point you should really be placing your commands in a shell script.

| improve this answer | | | | |
  • +1 for mentioning bash -c. – Timothy Martin Sep 30 '14 at 17:23
  • */1 * * * * "which whoami" gives "/bin/bash: which whoami: command not found". :-/ – Jake Sep 30 '14 at 17:24
  • Same for full path: "/bin/bash: /usr/bin/which whoami: No such file or directory" – Jake Sep 30 '14 at 17:24
  • That's due to the way the command field is being parsed by cron and passed on. Try it out with the code I put above. – Unix-Ninja Sep 30 '14 at 17:39
  • 1
    This is wrong. man 5 crontab: The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. – Patrick Sep 30 '14 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.