2

I'm about to descend into madness over a cronjob I'm trying to set up. I cannot for the life of me (after spending many hours/effort) figure it out, hope you can help.

I have a Python script at /Server/Home/Paul/Sys+Tech/sortphotos/src/sortphotos.py that takes some params and organizes files in separate/sub-folders. When I run this script directly it works just fine

I've set up a crontab for the user owning the python script (paul) that looks like this, but does not work

* * * * * echo $(date) "cron works" > /var/log/testingcron
* * * * * /usr/bin/python /Server/Home/Paul/Sys+Tech/sortphotos/src/sortphotos.py -r --sort "%Y/%Y %m" /Server/Multimedia/Photo/+inbox_paul/ /Server/Multimedia/Photo/
#* * * * * /bin/bash /Server/Multimedia/Photo/organize-inbox.sh

some elaboration

  • I've set a+rwx permissions on sortphotos.py

  • I've added the full path to the python executable and full path to the script

  • I've checked access/permissions of the source files

  • I've tried to set up a organize-inbox.sh bash-script containing the full execute command as an intermediary to run the sortphotos.py (commented out third line), also with lost of permissions on the .sh. This also works when directly running the organize-inbox.sh.

  • I've tried to set up the cron for the root user

I picked up some useful advice during my endless online searches, one of which was to test if cron is actually working by writing to a file. That's what the first cronjob is about. It writes a line in testingcron every minute, and every minute without fail I have a line with timestamp in said file. Unfortunately the second, and third command do not yield the desired result;

I'm looking forward to learning from you what I'm missing/messing up! Thanks, Paul

p.s. I'm running Ubuntu 20.4

terdon
  • 100,812
PaulDP
  • 31
  • 3
  • You will need to escape the percent signs - see for example Command with percent symbols not running in crontab – steeldriver Sep 22 '22 at 11:36
  • Thank you for your quick reply, however escaping the % did not work until now, I'm trying some other things around syntax (whole command in quotes) - I will update as soon as I know more. Also but the escape-fix should not be necessary when ran via the bash script but that doesn't work either (as far as my reasoning; cron just runs the sh and from there it's a shell script as any other, but i'm no expert) as you might have guessed :-) – PaulDP Sep 22 '22 at 12:43
  • I'd suggest looking at journalctl -xe -u cron, and redirecting the cronjob output/errors to a file (> /tmp/cron.out 2>&1 or similar) that you can examine for clues – steeldriver Sep 22 '22 at 12:47
  • was doing exactly that. journalctl shows something i don't get: sep 22 14:56:01 precision CRON[2769488]: (paul) CMD (/usr/bin/python /Server/Home/Paul/Sys+Tech/sortphotos/src/sortphotos.py -r --sort "%Y/%Y %m" /Server/Multimedia/Photo/+inbox_paul/ /Server/Multimedia/Photo/) i.e. the command without the escaping backslashes I definitely put in. I also tried wrapping the whole line in back-ticks . nothing; very strange – PaulDP Sep 22 '22 at 12:59
  • The command should be shown without the backslashes in the journalctl output (they force cron to treat the % characters as literals). If you omit the backslashes, you would see an incomplete command (truncated at the first un-escaped %) – steeldriver Sep 22 '22 at 13:10
  • I assumed as much, but nonetheless the journal shows the command has been executed but the files have not been sorted into their respective folders, as should have happened – PaulDP Sep 22 '22 at 13:13
  • I'm having one more go with the syntax, thank you for helping me so far, so wonderful – PaulDP Sep 22 '22 at 13:14
  • 1
    Please change the line to /usr/bin/python /Server/Home/Paul/Sys+Tech/sortphotos/src/sortphotos.py -r --sort "%Y/%Y %m" /Server/Multimedia/Photo/+inbox_paul/ /Server/Multimedia/Photo/ 2>/tmp/sortphotos.log and then check the /tmp/sortphotos.log file and report any errors you see. Also, you don't need echo $(date) > file, you can just do date > file directly, but that's not what is causing you problems. – terdon Sep 22 '22 at 13:20
  • Is there anything interesting about the cron job in /var/log/syslog? – Jos Sep 22 '22 at 13:25
  • @terdon Thank you, I just made the change and will report back in a minute, in the meantime, thanks for correcting my original question, is there a quick way to accept your edits? – PaulDP Sep 22 '22 at 13:25
  • You're welcome. And no need to accept, they have already been accepted. The reputation system of the site allows users with more than 3k reputation to directly edit, and since I have more rep than that, my edits don't need approval. – terdon Sep 22 '22 at 13:31
  • @Jos: nothing special in syslog @terdon some progress. I get an error from the script: it lacks permissions to move the file(s) Which I don't understand since I'm running the script as user:paul and it works, the crontab is for user:paul and the owner of the folder is also paul – PaulDP Sep 22 '22 at 13:37
  • dangit when inspecting the file permissions I see only -rw-r--r--. These files are 'uploaded' with Syncthing, so that might be the reason for the lack of permissions. But still, when running the python-script directly (and via .sh) everything works – PaulDP Sep 22 '22 at 13:42
  • I finally got it to work, turns out other did not have write permissions for the destination folder (+sub) - when I set this manually, everything worked. Still I'm not entirely happy with the setting/setup that needs write access for other to be able to run a cronjob that is set up under a specific user, maybe I'm wrong. For now, again; thank you all for helping me and (while doing) teaching me how to debug cron @terdon (2>file) and also @steeldriver for the syntax help. Cheers! – PaulDP Sep 22 '22 at 14:10
  • 1
    @PaulPetrutoni the crontab should be running under the same user, how did you set it up? Also, could you please post the solution as an answer so the system will show the question as "answered"? – terdon Sep 22 '22 at 14:11

1 Answers1

1

It turns out the permissions for the destination folder were not in tune with the rest of the setup.

root was owner of the destination folder 2022 and all sub folders. Since the cron-job and the python script were set up for user paul which did not have write permissions for 2022 the script failed. After setting chown to user:paul for the destination folder the cronjob runs as it should

Biggest takeaway; I should have checked permissions a lot better before posting, but thank you anyway for helping me understand cron (including stderr & stdout) for future needs.

PaulDP
  • 31
  • 3