You need to setup execute permission on a php script. You need to use chmod command to change file access permissions. [donotprint]
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | 1m |
chmod +x scriptname.php
Also make sure you add following line to top of php script (first line should be #!/usr/bin/php):
#!/usr/bin/php
If you are using FreeBSD unix os, use:
#!/usr/local/bin/php
#!/usr/bin/php is called as a shebang (pound bang). It execute php script using the interpreter /usr/bin/php. Save and close the file.
Setup and run php script as a cron job
Now add cron job by typing following command:
$ crontab -e
Sample outputs:
# run everday at 10:45 45 10 * * * /path/to/myphpscript.php
Save and close the file.
This entry is 11 of 16 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
- What is cron on a Linux or Unix-like systems?
- How To Add Jobs To cron Under Linux or UNIX?
- Verify crond Daemon And Cronjobs Are Running
- Start Restart and Stop The Cron or Crond Service
- List / Display All Cron Jobs
- Linux / UNIX Crontab File Location
- Change Crontab Email Settings ( MAILTO )
- Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
- At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
- Execute Cron Job After System Reboot
- Setup and Run PHP Script As A Cron Job
- Run crontab job every minute on a Linux or Unix-like system
- Running crontab (cron jobs) Every 10 Minutes
- Cron Job Script Execution on the Last Day of a Month
- Execute / Run crontab (cron jobs) every 1 minute
- Ubuntu create cron.log file
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 15 comments so far... add one ↓
🐧 15 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thanks Pal,
Its teach me more on setting the crontab on different server :)
My apologies for nitpicking but shouldn’t the cron job be,
45 10 * * * /path/to/myphpscript.php
for the Cron to run at 10:45 as the first field from the left starts with Minute..Hour and so on.
Swapneel is correct. A cronjob runs off of the following format for schedules.
minute hour day month dayofweek command
@Travid and Swapneel
Thanks for the heads-up!
Thanks. It was really helpful for me.
Big big big big thanks!!!
i tried passing arguments to a php script in UNIX
For example: 45 10 * * * /path/to/myphpscript.php -args filename=”hello.txt”
is there a way to pass arguments to a php script in unix
For example: 45 10 * * * /path/to/myphpscript.php -filename=”hello.txt”
I like this for running daily cron handling thanks :)
Great, thanks. Been do lazy to read up on this for a while, but this page makes it simple.
I run as a ./email_test.php
before I set as a cron job I run on a terminal,but then I had following error
“/usr/bin/php^M: bad interpreter: No such file or directory centos PHP
Search your php interpreter with help this command:
locate bin/php
if you need update location database, use:
updatedb
or (if it FreeBSD)
/usr/libexec/locate.updatedb
thanks a lot.
i run cron 00 11 30 08 4 php /path /script -args argumentName=”value”
it run.
thanks
Thanks, great help
I have a website http://www.noidasindhis.com from which I send emails to the registered Sindhis.
My program to send emails is in PHP and runs fine.
The site has been taken from dollar2host under Linux environment. I wish to send my mails at specified intervals using cron job but do not know the Linux command to do so.
Can any one help me?
Sahjwani