Benches Uncategorized

5

Correct crontask coding to run a php script

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I've got a simple php script setup that sends files via FTP. It's set to run every night and is setup the same way on multiple computers, yet on some it's not working.

It’s set to run as the root user and run the command *php /ftpup.php*

On most people’s computers it works, but on some it doesn’t and it’s setup the exact same way. Do I need to rewrite the command as */bin/sh php /ftpup.php*

It works fine when run from the command line.

I also thought about making this a launchd item instead of a cron task, but wanted to get the command right before going that route.

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It's hard to tell remotely since there are many things that could be wrong.

    The first thing to do is determine if the script is running at all, or whether it's running but some other error occurs. You can do this by adding log statements in the script, then examine the log file after the next scheduled execution. If there's no log then the script didn't run at all.

    That said, the commonest problem is one of paths. I'd start by including the full path to PHP and your script in the command ($PATH is often different in cront vs. user shells):

    /usr/bin/php /path/to/your/ftpup.php

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks dude, but it still didn't work. I just don't get why it works fine when run from the Terminal command line, but running the same command through a cron does not.

    Do you have a quick and dirty way of addingthe log statements to the command?

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Have you checked the local mailboxes on the machine for cron output?

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I just don't get why it works fine when run from the Terminal command line, but running the same command through a cron does not.

    Cron does not have the same set of environment variables as your terminal session. Also it runs /bin/sh as the shell which may be a differences from your terminal environement which is bash.

    Try running the following command from a cron job:


    printenv >/tmp/cron.env

  5. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Well I couldn’t get it to work, so I just created a user daemon plist file to run the php /ftpup.php script and voila!

    Thanks anyway

    A