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?

10
  • 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. Commented Sep 30, 2014 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. Commented Sep 30, 2014 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' Commented Sep 30, 2014 at 17:20
  • Re:CRUD that's (mostly) the default $PATH of EC2 instances. Commented Sep 30, 2014 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. Commented Sep 30, 2014 at 17:22

2 Answers 2

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
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.

7
  • */1 * * * * "which whoami" gives "/bin/bash: which whoami: command not found". :-/ Commented Sep 30, 2014 at 17:24
  • Same for full path: "/bin/bash: /usr/bin/which whoami: No such file or directory" Commented Sep 30, 2014 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. Commented Sep 30, 2014 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. Commented Sep 30, 2014 at 17:44
  • I just realized he was quoting the line, which is wrong. That's probably why it's failing. That being said, && doesn't work for me when I try passing it via cron. It may not execute the second statement for him after he removes the quotes. Commented Sep 30, 2014 at 17:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.