Get the url of cron job - cron

I have to find the url from where the cron job calls an API.
I tried <?=$_SERVER['HTTP_HOST'];?> in the cron file but it returned empty and also <? phpinfo() ?> did not help.
Cron command: * * * * * php -q /home/karenib/public_html/scr/cron.php

The <?=$_SERVER['HTTP_HOST'];?> will will not be populated when running it from a cronjob like php -q /home/karenib/public_html/scr/cron.php, since the file is not accessed via HTTP.
You can pass the host through params like
php -q /home/karenib/public_html/scr/cron.php -h=http://example.com
And access it like
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : getopt('h:');
However, if you still need the HTTP_HOST populated, you can use curl like
curl http://example.com/path/to/cron.php &> /dev/null

Related

Failing Cronjob in TYPO3 - Malformed parser URI - direct_mail

At the moment scheduler tasks of EXT:direct_mail are failing. The cronjob gets the wrong URL.
http:///usr/www/users/myuser/myproject/current/typo3 which breaks setting up a draft mail with direct mail schedular.
["code"]=>
int(1436717322)
["message"]=>
string(96) "The parsedUri "http:///usr/www/users/myuser/myproject/current/typo3" appears to be malformed"
["file"]=>
string(90) "/usr/www/users/myuser/myproject/releases/48/Web/typo3/sysext/core/Classes/Http/Uri.php"
["line"]=>
Our cronjob:
* * * * * /usr/bin/php74 -d allow_url_fopen='on' -d allow_url_fopen='on /usr/www/users/myuser/myproject/current/typo3 scheduler:run > /dev/null 2>&1
The problem is partly because TYPO3 uses the HTTP_HOST variable to create absolute URLs. This is available by default when invoked via CLI. Then it can happen that the path on the server is used instead of the domain. It was finally solved by setting the cronjob like this:
* * * * * env HTTP_HOST=www.my-domain.com /usr/bin/php74 -d allow_url_fopen='on' /usr/home/username/public_html/myproject/current/typo3 scheduler:run > /dev/null 2>&1
This ensures that the HTTP_HOST variable is available.

how to setup cron job for Lumen Applicaiton in shared hosting (using CPanel)?

I have created one command which will add some record to mysql database.
When I execute command with terminal, command is adding data to database.
Then I have modified Kernel.php, added command in commands array and scheduled task as below.
protected $commands = [
\App\Console\Commands\AddData::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('word:add')->everyMinute();
}
I have setup cron job in CPanel.
Command Setting : Once per minute.
Command added as :
/usr/local/bin/php /home/username/public_html/sitename/projectPath &&
php artisan schedule:run >> /dev/null 2>&1
But it is not working - i.e. not adding row to database table neither I am getting any error from in CPanel for cron job. am I missing anything or I am doing something wrong with it?
I have figure out the problem.
for this case, we need to give path of artisan file, so the command would be
* * * * * /usr/local/bin/php /home/username/public_html/sitename/projectPath/artisan schedule:run >> /dev/null 2>&1

cron job not running in goDaddy shared host for laravel 5.2

I am trying to run cron job in my laravel 5.2 application hosted in goDaddy shared host.
I have cPanel access and there I added a cron job, something like this:
* * * * * php /home/path/to/artisan schedule:run 1>> /dev/null 2>&1
But the issue is that the server is not calling schedule action in Kernel.php. The same works fine in my local system.
Can anyone point out the mistake please or suggest some way to accomplish this so that server runs the cron job so as to execute defined commands.
Add path of php binary to cron command.
* * * * * path/php /home/path/to/artisan schedule:run 1>> /dev/null 2>&1
Example : /usr/bin/php
Cpanel Cron job command in laravel godaddy and others are same
/usr/local/bin/php /home/ivlssadmin/public_html/inventoryproject/artisan OpeningStocks:openingstocks >> /dev/null 2>&1
Here what you change follow bellow :
/usr/local/bin/php /home/Your Host User Name /public_html/Sub Domain Name/artisan Command Name:Command Name >> /dev/null 2>&1
if you don't have sub domain you write only public html
Make sure you write the correct command in the schedule action. e.g. $schedule->command('send:followup')
Also check the timezone of the crontab if possible are you using utc timezone in your commands this is the default for most servers.

Implement cron schedular in linux

I am trying to execute a php script in my linux server. the script will run everyday at 8 am. I have uploaded my crontab in linux server and the php file update1.php Script in my crontab is given below. But this isnt updating my databse. where am i doing wrong ? Am i missing something here. Thanks in advance.
crontab
0 8 * * * http://www.mywebsite.com/update1.php
You can use --spider option of wget for this purpose.
0 8 * * * wget --spider http://www.mywebsite.com/update1.php
--spider indicates not to download anything (we just want to go through the pages, that’s all)
This line/code in your crontab:
http://www.mywebsite.com/update1.php
is not an execution of the script, it's just the url to it. You should download the php script, and put it locally on your linux server. That would make things simpler and much more reliable.
I would login to your linux server, use wget to download the script, chmod it to make sure it's executable, and mv it to wherever you want on your server:
wget http://www.mywebsite.com/update1.php -O update1.php
chmod 755 update1.php
mv update1.php /path/to/where/you/want/script
Then now that you have the script locally on your linux server, edit your crontab (crontab -e), and add the following line:
0 8 * * * /path/to/where/you/want/script/update1.php

Cronjob command not working, despite having the same format as other commands that DO work

Trying to add a new cronjob to my server using Plesk. I have two running already, but it's so long since I set them up that I can't remember why I did it the way I did. They work exactly as they should.
*/13* * * * &> /dev/null php -q httpdocs/forum/notifyreply.php
*/9 * * * * &> /dev/null php -q httpdocs/forum/notifytopic.php
However, when I add my new job using the same format, it doesn't seem to do anything. The script doesn't run and I get no e-mail notification to tell me that the script has run. The new job is as follows:
* * * * * &> /dev/null php -q httpdocs/crm/autoMessages/autoEmail.php
I'm running these on a Linux Virtual Server with Apache and using Plesk Control Panel. Hosting provider is 123-Reg.
Can anyone help?
Thanks!
UPDATE
So I've now removed the /dev/null line so I receive e-mail notifications and I'm getting the following error message:
/bin/sh: php: Permission denied
What I don't understand is why permission is being denied on this command but not on the other two...
Not sure what could have happened, but first thing I would do is to check if the permissions for the new php script are the same as what the old ones have. Do a ls -l on both httpdocs/forum/notifyreply.php and httpdocs/forum/notifytopic.php, and compare the permissions against what httpdocs/crm/autoMessages/autoEmail.php have.
You are not setting a User/Group for the cron command, better do it like this
* * * * * www-data www-data php -q httpdocs/crm/autoMessages/autoEmail.php > /dev/null 2>&1
(assuming that www-data is the correct User)
note that I put the error handling at the end.
Also, I think that you should use the full path; either you forgot to add a slash ( /httpdocs/.... ) , or do you have this script in the cron folder?

Resources