4

In my user's crontab, on a CentOS system, I have something like this:

58 12 * * * cd /home/joe/dev; echo `pwd` | logger

This outputs in /var/log/messages:

Feb 19 12:58:01 srv01 joe: /home/joe

Instead of outputting:

Feb 19 12:58:01 srv01 joe: /home/joe/dev

Why is the directory not kept when outputting pwd?

8
  • 4
    possibly because the path /home/joe/dev doesn't exist - so the first command doesn't do anything. – AD7six Feb 19 '15 at 18:53
  • 3
    What happens if you do sh -c 'cd /home/joe/dev; echo `pwd` | logger' instead? – Zoredache Feb 19 '15 at 19:01
  • 5
    @Daniel That's not for the user's crontab though, only the system-wide stuff. – Håkan Lindqvist Feb 19 '15 at 19:17
  • 2
    Guys, I feel bad, I managed to make it work however I don't know exactly how. But I think I might have made the mistake of not always exiting vim when I saved the changes to my crontab. I was used to edit the crontab manually and this time I was using crontab -e. – Martinos Feb 20 '15 at 0:21
  • 1
    @Martinos that should be in the question (and emphasised) as it makes every answer except BeepBeep's "I tried that and it works fine" answer meaningless, since they are explaining a problem that doesn't exist. – AD7six Feb 20 '15 at 8:16
6

The commands are being run independently, so it is not the same as expecting them to run in the same shell/environment. You would probably be better served by creating a shell script that performs the desired commands and then calling the script from the crontab.

Update: The above was a guess, and/or proposed possibilty (as a comment so tersely and aggressively pointed out). So I decided to test on CentOS 6.6 and... I was wrong... The following crontab entry worked properly:

* * * * * echo \`pwd` |logger; cd /home; echo `pwd` |logger;

So that should have worked for you... I still maintain that you would be best serverd by calling scripts not individual commands in your crontab... but that is a preference (so please be kind in the comments...).

5
  • That sounds like a guess, to fit "observed" behavior. – AD7six Feb 19 '15 at 19:01
  • 2
    Lots of 'answers' are guesses here. That isn't necessarily a bad thing. An educated guess can be very helpful in leading people to solving their problems. – Zoredache Feb 19 '15 at 19:04
  • @BeepBeep is right. If you use ; in cron, there are two independent commands running. If you want to concatenate them, use &&. – ThoriumBR Feb 19 '15 at 19:24
  • The line that you wrote in you post works. However for some reason cd to my /home/joe/dev doesn't work. It seems to be a permission issue even the user running the cron is the same as the owner of the /home/joe/dev. – Martinos Feb 19 '15 at 20:34
  • As the only sane answer (especially in light of the question being a misunderstanding) : +1. Please correct the escaped backtick, and preferably put the update first with emphasis - it doesn't matter to future readers you wrote something different originally. – AD7six Feb 20 '15 at 8:22
5

The following should work:

58 12 * * * (cd /home/joe/dev; echo `pwd` | logger)

By wrapping the commands in parentheses, you should force them all to run in the same subshell.

1
  • Parentheses are not needed. All commands are already running as in the same subshell. – Jakub Kukul Jun 28 '20 at 15:22
2

As per this Unix&Linux SE answer, you can replace ; with && as different versions of cron across different Linux distributions handle this type of functionality in different ways:

cd /home/joe/dev && echo `pwd` | logger
3
  • 2
    "need"? The linked answer says nothing about ; not working. "My car doesn't start" "My blue car starts, you therefore need a blue car" is not a useful answer. (Note that the Asker comments that the question is effectively nonsense). -1. – AD7six Feb 20 '15 at 8:19
  • 2
    @AD7six point taken, answer edited :) – Craig Watson Feb 20 '15 at 8:22
  • 1
    Thanks for taking my comment constructively, DV removed. different versions of cron across different Linux distributions... - I don't know of any version of cron, on any distro, that doesn't understand a semicolon, and of course ; and && are not logical equivalents. – AD7six Feb 20 '15 at 8:27

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.