Running a Cronjob on 1&1 Shared Hosting [Laravel] - php

In my Laravel application, I have created a scheduled task, as defined in a previous question that I asked. It essentially grabs data and updates a few tables.
I followed a help article on 1&1 about creating cronjobs.
To summarise this article it said the following.
SSH to your server, that you want to set up the new cronjob on, use whereis php to find the location of your PHP Binary, then use the crontab command to access the crontab and add a new job.
This will require the PHP path and the path to the script that is to be run.
The issue is when I run that command I get the following.
No crontab for [username]
So, I then Googled and it seems some shared hosts don't allow you to use or even access the crontab.
On my shared hosting server the path to php-7.1-cli is as follows /usr/bin/php7.1-cli
Now, Artisan is part of Laravel and requires a higher version of PHP than what is found in \usr\bin\php
Given this, for the cronjob I thought I'd be able to do something like this:
* * * * * /usr/bin/php7.1-cli /path/to/artisan schedule:run >> /dev/null 2>&1
Or
* * * * * /usr/bin/php7.1-cli /path/to/mynewable/artisan schedule:run >> /dev/null 2>&1
My folder structure is as follows:
root
/mynewable
But when I run either of these commands, nothing happens.
1&1 also has an inbuilt cron job creator but this only allows me to specify a URL path to a file that would run the necessary script.
Am I being prevented from using crontab? If so, giving a URL seems impractical as the user wouldn't need to validate, but making a path in web routes to run the Laravel scheduler seems a bit of a security hole.

You can create a executable cron.php in your public folder of laravel.
exec("/usr/bin/php7.2-cli -f /homepages/x/xxxxx/htdocs/artisan schedule:run 2>&1", $out, $result);
if(!empty($out)) {
echo implode("<br>\n\n", $out);
}
Now you can call http://example.org/cron.php
Of course you can put the cron.php in a protected directory so no one can call the cron.php.

Related

Laravel cron on Digitalocean

I have set up Laravel scheduler to run my custom commands at specific time. Now I wanted to set up cron on Digitalocean server to trigger schedule:run each minute to check if something is scheduled at the given time.
After initial SSH-ing to server, I have run crontab -e and added the following line to it:
* * * * * php /var/www/Laravel artisan schedule:run >> laravel_cron.log
but the problem I'm facing is that I don't see anything written in laravel_cron.log, but it does get created, so now I have no idea whether my commands will actually be ran.
To test it out, I have tried entering php /var/www/Laravel artisan but I get no output in command line.
If I change the route to say xyz/www/Laravel it is saying that it can't find it, so I guess the route is set up fine. Also when I manually go to the Laravel folder and run php artisan without the route in the middle, I get the standard output.
I believe the command php /path/ artisan schedule:run does not return any output.
If you want to log the output of a task, you can use sendOutputTo or emailOutputTo
e.g.
$schedule->command('foo')
->daily()
->sendOutputTo($filePath)
->emailOutputTo('foo#example.com');
More examples can be found here

Setting up a Cron Job on CPanel to executes a PHP script

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

How to run a cron job in codeigniter without using wget?

I want to run a cron job without using wget in CodeIgniter.
I am using it like this:
*/1 * * * * wget http://assurance.com/controller/function
It works successfully, but I do not want to use wget.
Is there any another way to run this CodeIgniter script?
You can try and use something like this:
* * * * * /usr/bin/php /pathToTheApp/controller/function
But of course the /usr/bin/php should be your path to the PHP binaries and pathToTheApp should be the absolute path to your CI application.
If you have local shell access on the host, add it to your crontab there.
I needed to do this exact thing recently and couldn't find a complete solution. So I'll try to provide one here.
I used "bash shell" because I wanted to make a command line (CLI) call to my controller into of an http (note: I'm using an ubuntu/linux server.
There are three main parts:
The cron call
The shell script
The controller function
Cron:
in your server cli type this to access crontab: crontab -e
then add your cron call: * * * * * bash /path/to/script/test.sh
(note: I created a folder in my site root called cron, so my path would be:
/var/www/website/cron/test.sh
Shell script:
In that cron folder we made, create a file called " test.sh "
In the file put:
#!/bin/bash
cd /path/to/site
/usr/bin/php index.php controller function
That's it
cron sets up the timer to call the file
shell calls the controller from the CLI
you'll now be able to use $this->input->is_cli_request() in your function for added security.
public function cronTest()
{
if($this->input->is_cli_request())
{
//code goes here;
}
}
Hope this helps save you some time, this took me way longer than expected :)

PHP Cronjob changing sh root

Some setup background first:
I have a cronjob which runs a PHP file called worker_cronjob. All the file does is download my worker from git and the cronjob in cron.d looks like:
*/1 * * * * ubuntu /home/ubuntu/worker_cronjob >> /home/ubuntu/worker.log
It includes the worker_despatcher file
Which fires off a child process with (ROOT being an abolsute path to my directory):
$PID = exec(sprintf("%s > %s 2>&1 & echo $!", "php ".ROOT."/worker/encoder.php".$arg_string, ROOT."/worker/encoder.log"));
The problem is that under a cronjob this method is changing the way system commands are run, more specifically sh. So when I run a command like:
ffmpeg
It returns:
sh: 1: ffmpeg: command not found
After trail and error I have discovered this only happens from the cronjob, somehow it is changing the way directories are set, much like chrooting without me calling chroot.
I have looked at other threads and it says it use full paths when creating cronjobs and running files, however it's not my files that's the problem and they are all referenced via absolute paths, it is running installed programs where I get problems.
Does the absolute path apply to installed apps as well or is there a way to break this functionality to give me back the ability to just run a command with one word?
The behind reason is that cronjobs are run by the system, so they know nothing about your shell or user environment variables. You can say they run in a minimal environment.
A great detailed answer can be found in Reasons why crontab does not work.
Another way not shown in the above link resource is:
* * * * * PATH=/usr/bin; command >> /var/log/command.log

Cron job is not giving required result, but accessing same file through browser does

I need to run a php script to generate snapshots using CutyCapt of some websites using crone job, i get websites' addressess from database and then delete this record after generating screenshots.
i used */5 * * * * /usr/bin/php -f /path/generate.php
it didn't worked for crone job but if i access the same file using browser it works fine, and if run this script using command php from command line it also works fine.
then i just created another file and accessed the url using file_get_contents; added this file to crone job it worked fine for some days but now this approach is also not working. i don't know what happened. i didn't change any of these files.
I also tried wget command to access that url but failed to get required out put.
my crontab is now looks like this
*/5 * * * * wget "http://www.mysite.com/generate.php" -O /dev/null
Strange thing is that crone job executes fine it fetches data from database and deletes record as well but does not update images.
Is there any problem with rights or something similar that prevents it to generate images using crone job but not when accessed using browser.
Please help i am stuck.
I don't know what your script is doing internally, but keep in mind that a user's cron jobs do not inherit the users environment. So, you may be running into a PATH issue if your php script is running any shell commands.
If you are running shell commands from the script, try setting the PATH environment variable from within your php script and see if that helps.
is there any user credintials on this page , such as Basic authentication ?
if so , you have to define the user name and password in wget request like
wget --http-user=user --http-password=password "http://url" ?
and try another solution by running yor script from php command line
so your crontab could look like
*/5 * * * * /usr/bin/php -f /path/to/generate.php
try this solution it will work and it is better than hitting the server to execute background operations on your data
and I hope this helps

Categories