Score:0

How Do I Get Crontab to Run a Whole Directory of PHP Files?

in flag

I've created a little program that will generate php files and place them into a '/crons' folder to allow cron jobs to execute the files. These php files will be generated and named dynamically based on user input. So I was wondering how I could get Crontab to execute ALL of the php scripts that are in that '/crons' directory, simultaneously. Is it some kind of regular expression (regEx) with a wildcard to run the entire directory, as shown below (something similar to it)?

*/1 * * * * php /var/www/mywebsite.com/path-to-my-awesome-php-scripts/crons/*

What exactly would I need to do? Does this require a bash or shell script? (I have no experience with bash or shell). Thanks.

Paul avatar
cn flag
Is that not working?
Aaron Esteban avatar
in flag
Oh, I havent actually tried that, but does cron accept a regEx wildcard at the end of a directory? I will give it a try. Just wondering if this is supposed to work and if not, is it something similar to this?
Paul avatar
cn flag
You can see in `/etc/crontab` it starts `SHELL=/bin/sh`.
Aaron Esteban avatar
in flag
Ok, so I've tested it and it only seems to run 1 php file instead of the 2 php files that I have in the directory. Maybe the cron is only able to select the first file.
Chris avatar
it flag
With no experience with bash or shell, I would create another php script that will handle your logic with `path-to-my-awesome-php-scripts/crons/*` and you simply call it in cron job `*/1 * * * * php /var/[...]/path-to-my-awesome-php-scripts/cronjob.php`
Aaron Esteban avatar
in flag
Right, and I have though of that Chris such as a master file with includes in it, but I was wondering if there was already a simple way to do it with a crontab command.
Paul avatar
cn flag
If there was a problem running the other scripts, there should be errors?
Score:1
jp flag

When you run php *.php then PHP interpreter executes only the first file. You can check it outside of cron environment.

You can use run-parts to run all executables in a specific directory. See run-parts man page

You'll need to make your PHP files executable with a proper #! header and executable bit. Your crontab should look like

*/1 * * * * run-parts /var/www/mywebsite.com/path-to-my-awesome-php-scripts/crons
Aaron Esteban avatar
in flag
Hi Alex, can you show me an example of how to make proper #! headers and an executable bit? I've never seen this before in php. If you can show me a quick example with proper syntax of what you're referring to I can get the visuals and write it properly from that point on. Thank you for your response.
jp flag
@AaronEsteban, https://stackoverflow.com/questions/21731745/how-to-add-shebang-with-php-script-on-linux
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.