Show
Cron is a time-based scheduling service in Linux / Unix-like operating systems. Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory. In this article I will show you how to execute PHP script in cron job. Create a PHP ScriptFirst of all create a php script like below. # cd /var/www/web # vim test.php Save and close file. Configure Corn TaskIn the following example, the crontab command shown below will activate the cron tasks automatically every five minutes: */5 * * * * /usr/bin/php /opt/test.php In the above sample, the */5 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every “five” minute. The other figures represent, respectively, hour, day, month and day of the week. * is a wildcard, meaning “every time”. Find PHP LibraryType the below command to find the php library. # whereis php You will get some output like below: php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz Specify correctly the full path in your command. Cronjob EntryNow type the below command to scheduled task and set cron like below. # crontab -e */5 * * * * /usr/bin/php /opt/test.php Now type wq! to save it. Thank you! for visiting LookLinux. If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook. You may also likeAbout the authorHi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section. Working with cron jobs manually is most commonplace with low volumes of cron jobs. But what if you’re working with high volumes of cron jobs? PHP is the answer! Why PHP? Simply because it can automate the tasks needed to add, remove and list cron jobs. In this tutorial, you will learn how PHP can save time by automating adding, removing, and listing cron jobs for a Linux system. Ready? Jump right in!
PrerequisitesThis tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:
Creating the Crontab FileBefore you can manage cron jobs, you first need a file to store those cron jobs. All cron jobs are stored in one system-wide crontab file. Open your terminal and run the command below to change directory to
Now run the command below to create the crontab file (
Adding Single Cron JobsNow that you’ve created the crontab file, you can start managing your cron jobs. But you first need to focus on adding them via PHP. How? You’ll create and execute a PHP script to add cron jobs. Cron jobs are scheduled with five parameters, as shown below. Each parameter has a default value of *, which indicates that no value was specified for that parameter, whether for hourly, daily, weekly, or monthly cron jobs.
1.
Create a file in your text editor, copy/paste the code below and save it in the /usr/local/jobs directory. For this example, the file is named newjob.py, but you can name it differently. The code below prints a text ( 2. Next, create a PHP script file, then copy/paste the code below to that PHP file, and save it in the /usr/local/scripts. For this tutorial, the script is named add.php.
The code below adds the newjob.py file as cron job in the crontab file.
3. Run the command below to execute the
4.
Finally, run the below Listing Cron JobsPerhaps you want to keep track of the cron jobs you’ve added, so you can either update or remove them. If so, your first step is to list the existing cron jobs. Listing cron jobs come in handy to keep track of the cron jobs you have. Create a new PHP script, give it a unique name, and paste the code below to the script file. For this example, the script is named list.php. The below code’s
Now, run the command below to execute the list.php script. Executing PHP script to List Cron JobsListing an Array of Cron JobsPerhaps you want to print the list of cron jobs in an array instead of a plain list. If so, you’ll need the Replace the content of the list.php file with the code below. Executing the code below lists an array of cron jobs, and also prints the string length of the cron job.
Now run the command below to execute the As you see below, the code outputs the list of cron jobs in an array, while also showing the string length. Listing Cron Jobs in ArrayRemoving Cron JobsNow that you have the list of existing cron jobs, you get to decide what you want to do with each cron job. Perhaps you have cron jobs that you want to remove. If so, you’ll specify the cron job to remove in a string variable. 1. Create a new PHP script called remove.php for this example, then add the code below to the script. You can name the script file differently as you prefer. The below code removes a specific cron job from the crontab file.
2. Run the following command to execute the 3. Finally, run the Adding and Removing Multiple Cron JobsFrom adding a single cron job, you can also add multiple cron jobs, perhaps when you have daily tasks you want to automate. How? By adding a for loop in your script. Adding and removing multiple cron jobs share a similar approach, but the script to remove multiple cron jobs has more use of variables, as demonstrated below. 1. Replace the content of your add.php file with the code below. Executing the code below adds multiple cron jobs to the crontab file by paddling through an array.
2. Next, rerun the command below to execute the 3. Run the below 4. Now, replace the content of your remove.php file with the code below, which removes multiple cron jobs that you defined. Similar to adding multiple cron jobs, the same approach applies in removing multiple cron jobs, but with additional variables.
5. Run the following command to execute the 6. Finally, run the If you’ve successfully removed the cron jobs, you won’t get an output like the one below. Cron Jobs are RemovedCreating Functions to Manage Cron JobsSo far you’ve seen how can add, remove and list cron jobs in separate scripts. But to effectively manage cron jobs, you can create functions in a single script instead of running three separate scripts. Create a new script file and paste the code below. Give the script any name you prefer, but for this example, the script is named multi.php. The code below adds, removes, and lists cron jobs with three separate functions that you can call at the bottom of the script.
Now, run the command below to execute the Below, you can see the output of Preventing Memory OverloadYou’ve seen PHP’s capabilities in managing cron jobs, but having too many cron jobs consumes memory. How to avoid memory overload? Set a count limit to how many cron jobs you can add to the crontab file. 1. Add the following function to the multi.php script. The function below counts all cron jobs by counting all lines inside the crontab file and prints a message to inform you of the current cron job count.
2. Next, call the 3. Run the command below to execute the As you can see below, there’s a message that tells you the current cron jobs count. Executing the multi.php which Outputs the Current Cron Jobs CountBut perhaps you want to automatically terminate the script if there are too many cron jobs running simultaneously. If so, adding an 4. Replace the The function below reads the number of lines inside the crontab file, and based on that number, decides whether to terminate the script or not.
5. Finally, execute the In the output below, you can see the message that tells you the current cron jobs count (63). Since the cron jobs count is more than 50, the script then terminates automatically and not proceed in adding cron jobs. Terminating Script when reaching the Cron Jobs Count LimitConclusionThroughout this tutorial,
you’ve realized that PHP provides a way to automate cron jobs with the Now, will you consider PHP the next time you need to manage cron jobs for your projects? How do I run a cron job in a PHP script?How to run PHP scripts from cron jobs. Copy curl http://example.com/script.php. In this command, curl retrieves the web page, which then runs the PHP script. ... . Copy php -q ${HOME}/public_html/script.php. In this example, the PHP command-line interpreter runs the script. ... . Copy cd ${HOME}/public_html/; php -q script.php.. What is use of cron job in PHP?The App Engine Cron Service allows you to configure regularly scheduled tasks that operate at defined times or regular intervals. These tasks are commonly known as cron jobs. These cron jobs are automatically triggered by the App Engine Cron Service.
How do I manually run a cron job?Manually creating a custom cron job. Log into your server via SSH using the Shell user you wish to create the cron job under.. You are then asked to choose an editor to view this file. #6 uses the program nano which is the easiest option. ... . A blank crontab file opens. Add the code for your cron job. ... . Save the file.. |