1

Hope someone can help me out here.

I've written a python script that I want to run under CRON every 5 minutes. In keeping with the general security practice of least privilege, I:

  • created a user "custom" to use for custom scripts
  • created a group "custom" (don't want anything running under nobody to have access) and;
  • put the script (monitor) in /home/custom/bin

The shebang in the script is:

#!/usr/bin/env python2.7

The only permission given to user custom is:

Inherited from   Name   Description                  Action
                User - System: Shell account access   Indicates whether the user is able to login for example via SSH.

Running the script with the command /home/custom/bin/monitor from the command line works regardless of the current working directory.

I tried doing cd / first to make sure it wasn't a path issue, and the script ran correctly.

The script writes 2 files on the first run, and subsequent runs append to those files in the directory /home/custom/bin/mondata

I installed the CRON package and created the following entry:

*/5 * * * * custom /home/custom/bin/monitor

(There doesn't seem to be an "Apply Changes", so I'm assuming that I don't need to reboot or do anything to load the changes.) (The above entry and many others are displayed in the WebGUI, and I know that several of those other cron jobs are running.)

After waiting for enough time for the script to run, I checked for output and there was nothing.

Changing custom to root for testing purposes (Just in case the issue was a permission problem) doesn't fix the problem.

Here are the relevant file permissions:

[2.3.2-RELEASE][custom@local]/home/custom/bin: ls -laR ~
total 52
drwxr-xr-x  4 custom  nobody   512 Aug  7 00:14 .
drwxr-xr-x  4 root    wheel    512 Jul 27 15:24 ..
drwxr-xr-x  3 custom  custom   512 Aug  7 00:14 bin

/home/custom/bin:
total 20
drwxr-xr-x  3 custom  custom   512 Aug  7 00:14 .
drwxr-xr-x  4 custom  nobody   512 Aug  7 00:14 ..
drwxrwx---  2 custom  custom   512 Aug  7 00:07 mondata    <-Script output goes here
-rwxr-xr-x  1 custom  custom  4663 Aug  5 22:44 monitor    <-The script

/home/custom/bin/mondata:
total 8
drwxrwx---  2 custom  custom  512 Aug  7 00:07 .           <-NO OUTPUT! (I deleted the files manually after successful tests)
drwxr-xr-x  3 custom  custom  512 Aug  7 00:14 ..

I checked for an error message in: Status / System / Logs / System / General - Nothing found.

I even tried temporarily changing the permissions on /home/custom/bin/mondata to 777 and setting the cron user to root (totally unacceptable security practice just for testing, but even that didn't work.)

I have no way of knowing if the script is running and the file writes are being denied for some reason, or if the script isn't running at all.

BTW, where is the cron tab? When I run crontab -l as root, I get crontab: no crontab for root, but I KNOW cron jobs are running. (I have the daily mail report running.)

Any suggestions - even for troubleshooting to know if the script is running would be helpful.

Thanks.

Jongware
  • 22,200
  • 8
  • 54
  • 100
user73383
  • 89
  • 1
  • 7
  • If you install the 'Cron' package on pfsense, then you can view/edit the cronjobs from 'Services -> Cron' from the web interface. – mwfearnley Oct 17 '17 at 11:54
  • 1
    An the big question: how to see cron logs from the web interface? – sorin Apr 06 '18 at 08:38

1 Answers1

1

The issue was that python was not executing.

The so called "portable" shebang that worked at the command line -

#!/usr/bin/env python2.7

- DOES NOT WORK from cron.

I created the following file as /home/custom/bin/tcron

#!/usr/bin/env python2.7
import os
os.system('/usr/local/bin/minicron')

When run from the command line, it put the minicron error into the log every time it is run, but did nothing when run from cron.

I changed #!/usr/bin/env python2.7 to #!/usr/local/bin/python2.7, and now it works.

I don't know if this is intentional that #!/usr/bin/env python2.7 doesn't work from cron, but for now I'm not going to worry about it.

I hope by documenting this it might save somebody else the same trouble, and if it's a bug that should be reported, someone who knows how to do that will do so.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
user73383
  • 89
  • 1
  • 7
  • I think the problem is that cron can use a different `$PATH` from your login shell, which doesn't contain `python2.7`, so `env` won't find it. You can check your cron environment with https://stackoverflow.com/a/2546509/446106 – mwfearnley Feb 20 '19 at 15:08