How to run a PHP script using a Cron job - php

I'm having a PHP script which I want to run every 2 minutes using a Cron job in Ubuntu. I'm getting the following error:
bash: */2: No such file or directory
I've completed all steps below to set up a Cron job.
Please help me to resolve the error, I don't understand what I'm doing wrong.
1) Write this command in my terminal: where is php
Output:
php: /usr/bin/php /usr/bin/X11/php /usr/share/php /opt/lampp/bin/php /usr/share/man/man1/php.1.gz
2) Run a PHP script every 2 minutes:
*/2 * * * * /usr/bin/php /opt/lampp/htdocs/kyrill/filetest.php
Output:
bash: */2: No such file or directory

Seems like you are executing the crontab directive.
Execute
crontab -e
to edit your cron jobs. Then add this line at end of the file
*/2 * * * * /usr/bin/php /opt/lampp/htdocs/kyrill/filetest.php

try
0/2 * * * * curl http://[your_id:port]/kyrill/filetest.php

Related

Cron tab doesn't run most of tasks

I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh

Symfony2 cronjob environment not found

I am trying to run a Symfony2 command with a cron job but I get an error that the environment is not found. Here is my cron job:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console >> /usr/lib/myApp/forumLog.txt 2>&1
For now I am just trying to make app/console work and the expected output is a list with all commands. The error that I get is:
[Symfony\Component\Debug\Exception\ContextErrorException]
User Error: The environment was not found
Do you have any idea what is wrong and what is the correct way to run symfony2 commands through cronjob?
In my own Symfony-console running cronjobs, I usually have cron run a shell script, that first changes into the apprppriate directory, and then runs the console command.
Here's an example that has been running for a year or two:
File: /etc/cron.d/systemChecks (run a shell script as user: www-user)
10 7,19 * * * www-data /var/www/dir.../bin/liipMonitor.sh
File: /var/www/dir.../bin/liipMonitor.sh
#!/bin/sh
# Running at 7:10 and 19:10
cd /var/www/dir.../
bin/console --env=prod monitor:health --group=cli -q
I put the cron setup into their own files in /etc/cron.d but much the same would apply in any other crontab file. The shell script changes directory to the base directory of the project, and then runs bin/console.
Set the --env parameter in the cronjob command, like that:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console --env=prod >> /usr/lib/myApp/forumLog.txt 2>&1

How to run a cron job on OSX for php using MAMP

I've been at this for hours and have tried everything with no luck.
I am basically trying to run this http://docs.phpservermonitor.org/en/latest/install.html#setting-up-a-cronjob
I'm using MAMP and my localhost is a custom folder under User/username/localhost/servercheck
What I've tried so far is.
crontab -e
added */1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
And when I type crontab -l to see if its loaded I see the following.
*/1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
But the script does not run. I even tried a simple script that writes to a file. Still nothing. For some reason the cron job doesn't execute. Any ideas?
You need to call MAMP’s PHP executable, which will depend on on the version of PHP you’re running. For 7.2.1, the following below would be the proper path
*/1 * * * * /Applications/MAMP/bin/php/php7.2.1/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php

Run url ervery 2 minutes using cron jobs in php ubuntu not working

i want to execute a url for every 2 min in php so i followed the steps
1) crontab -e
2)select nano editer
3)insert the line
* * * * * /usr/bin/GET http://www.gmail.com/sendMail
After saving the file I got the response
no crontab for root - using an empty one
crontab: installing new crontab
Is there any wrong with the command.
Can any one help me out.
you can use curl for hitting URL
follow following steps :
crontab -e
add following line to the file
*/2 * * * * /usr/bin/curl www.google.com
to know more about curl you can do
man curl

cronjob is not working Linux

I want a script file to run once every minute.. I've written this command.
* * * * * php -q /home/<username>/public_html/cron.php
But, this cronjob is not working. whenever, I try to open this file cron.php in browser, it works fine.
I'm using Linux OS. Is there a way to debug it in order to come to know the error?
If you're using Ubuntu as I am, use the full path.
* * * * * /usr/bin/php -q /home/<username>/public_html/cron.php
Have you added an empty line (new line) after your cronjob?
To debug:
Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.
* * * * * php -q /home/<username>/public_html/cron.php; ls -la >>/var/log/cronrun 2>&1
This will capture anything from the Unix command.
A couple of additional hints: Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;
set
set -x
For cPanel, you may want to test curl (in case it's installed on your server):
curl --silent --compressed http://www.your-domain.com/cron.php
So it should look similar to: http://grabilla.com/0450d-93d93a32-02ab-457c-ac1c-d2883552a940.html#
You may also want to try removing the -q from your command and see if it helps.
* * * * * php /home/<username>/public_html/cron.php
*/1 * * * * /usr/bin/php -q /home//public_html/cron.php
Add the above line to the crontab file and run it . It will add a cronjob every minute

Categories