6 Replies - 273 Views - Last Post: 18 September 2018 - 05:41 PM

#1 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 05-June 09

Setting up Cron

Posted 17 September 2018 - 02:58 AM

Ok, so I need to use cron to run a script for my site at a certain interval of minutes and I've Googled many pages and can't really grasp how it works. So for one, can I run a custom PHP script for my cron job or does it have to be a shell script or executable file? I thought it worked like running a command line operation so for something simple I put a new file in cron.d and its contents were "@reboot root service nginx start" to automatically start my server software whenever the computer is rebooted but when I rebooted my computer it didnt work. I feel this is more confusing to me than it should be, could someone point me in the right direction? For example it should be obvious what I'm trying to do with that command so the correct way to use cron to start nginx on reboot would be a start and also the format I'd use to run a script that runs periodically in terms of minutes such as verifying a payment or if a verification link was visited in the correct time even if the user didnt visit the page.

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Setting up Cron

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6727
  • View blog
  • Posts: 31,140
  • Joined: 10-May 07

Re: Setting up Cron

Posted 17 September 2018 - 10:15 AM

The crontab is a server process, not part of php. It's a scheduler to run services or scripts on a timed basis. That said, your crontab needs to be of the user that it will run the script as. So if you want to restart nginx, the entry will need to be for a user that has the permission to run that command.
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2208
  • View blog
  • Posts: 6,676
  • Joined: 15-January 14

Re: Setting up Cron

Posted 17 September 2018 - 05:44 PM

Quote

So for one, can I run a custom PHP script for my cron job or does it have to be a shell script or executable file?

Any command that works on the command line. If you want to run a PHP script, you run PHP and pass it the script to run, e.g. php -f /home/user/public_html/cron/script.php. Note that PHP behaves differently if executed on a command line. For example, things like $_GET, $_POST, $_SESSION, etc are not available, they are only available through a web server. Some PHP options also change, for example the max_execution_time is unlimited on the command line.

Quote

I thought it worked like running a command line operation so for something simple I put a new file in cron.d and its contents were "@reboot root service nginx start" to automatically start my server software whenever the computer is rebooted but when I rebooted my computer it didnt work.

Cron isn't the tool to use to run things once on startup, cron is for running on a timed schedule. There are other methods if you want to run commands on boot.

Quote

a script that runs periodically in terms of minutes such as verifying a payment or if a verification link was visited in the correct time even if the user didnt visit the page.

Those aren't really great uses for cron either. Payment verification, for example, is based on an event (when the payment actually gets verified, which hopefully should be part of some API that will contact your system to tell you that it's verified, and you update the database when that event happens). For a verification link, you store in the database along with the link details the time it was created, and if someone tries to access it you look up the start time, add whatever interval, and see if it's expired. If you want a list of links that have expired you just get all of them that are over the time limit that have not been verified.

Things that we use cron jobs for are to periodically check an email queue to send emails in the background. So, instead of sending batches of emails immediately, we just queue them up in the database, and have a cron job that runs every minute to check if there are new emails to send in the background instead of the user waiting for them all to be sent. We also have one that runs every midnight, to do any tasks which should be done once per day (there are a lot in our application, but once-per-day tasks vary by application). There are others to do things like checking email accounts to see if there are any incoming emails that need to be processed automatically, where we don't have a system to send our application a notice when a new email arrives so we check it manually every so often. Any other time-consuming tasks that people might want to do, but you don't want to have them sitting in the browser waiting until it's done, are good for cron. Maybe you want people to be able to generate large reports which will take minutes, but you don't want their browser to be locked up until the report is finished, so you schedule the report, have a cron job check to see if reports need to be generated, and send them an email with the report when it's finished.
Was This Post Helpful? 0
  • +
  • -

#4 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 05-June 09

Re: Setting up Cron

Posted 17 September 2018 - 11:19 PM

View Postno2pencil, on 17 September 2018 - 11:15 AM, said:

The crontab is a server process, not part of php. It's a scheduler to run services or scripts on a timed basis. That said, your crontab needs to be of the user that it will run the script as. So if you want to restart nginx, the entry will need to be for a user that has the permission to run that command.


I ghaven't bought a live VPS yet, I'm developing on a LEMP stack on Ubuntu. I did some Java before and figure I could just write a Java app that gets cron'ed at different intervals. I also need to get data from an API on a certain interval and save it where my PHP application can read the data such as the Java app using cURL to get the JSON object then store needed data in the server. I'm having a hard time grasping the syntax of cron and what the difference between the crontab (crontab -e) and cron.d folder in the /ect/ directory. I figure if I get it all working properly on my development computer I could just get a Debian server and use the same settings and installed applications. I have several things that need to be done periodically on my application without needing a page visited by a user.

This post has been edited by trickstar34: 17 September 2018 - 11:22 PM

Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6727
  • View blog
  • Posts: 31,140
  • Joined: 10-May 07

Re: Setting up Cron

Posted 18 September 2018 - 05:03 AM

You run crontab -e to edit the crontab for that user. So say your Java app accepts 2 arguments, user & password. What I would do is write a shell script to run, & then log the output of running that command :

#!/bin/bash

user=dic_user
pass=secret

java command $user $pass >> /var/log/java_app.log




Lets say we named this shell script java_script.sh

Then load crontab editor : crontab -e

0 12/24 * * * java_script.sh

This is minute, hour, day (of the month), month day (of the week)

So this says, at the zero minute, of the 12 & 24 hour, every day, every month, run the script.
Was This Post Helpful? 0
  • +
  • -

#6 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 05-June 09

Re: Setting up Cron

Posted 18 September 2018 - 02:50 PM

View Postno2pencil, on 18 September 2018 - 06:03 AM, said:

You run crontab -e to edit the crontab for that user. So say your Java app accepts 2 arguments, user & password. What I would do is write a shell script to run, & then log the output of running that command :

#!/bin/bash

user=dic_user
pass=secret

java command $user $pass >> /var/log/java_app.log




Lets say we named this shell script java_script.sh

Then load crontab editor : crontab -e

0 12/24 * * * java_script.sh

This is minute, hour, day (of the month), month day (of the week)

So this says, at the zero minute, of the 12 & 24 hour, every day, every month, run the script.


So whats the purpose of the cron.d directory then? I noticed theres a file in there that has to do with PHP and it says it has something to do with sessions in the comments.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2208
  • View blog
  • Posts: 6,676
  • Joined: 15-January 14

Re: Setting up Cron

Posted 18 September 2018 - 05:41 PM

It looks like /etc/cron.d just has a bunch of things to run, I assume cron looks in there for additional stuff to do. On one of my servers I see a few things that cpanel added and some other stuff.

Just a note of caution in case you're a quick, but occasionally incorrect, typist - the genius behind crontab decided to make the flags for edit (-e) and delete (-r) one key apart.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1