I've looked over many SO threads on cronjobs and can't figure out why my simple cronjob only runs once and doesn't repeat after each minute.
here's my cronjob setup:
html$ crontab -e //directory where my web application resides
*/1 * * * * php index.php cronjob everyMinute //command inside crontab
html$ php index.php cronjob everyMinute //run script
Here's my PHP script (my web application uses the Codeigniter framework but that doesn't seem relevant):
function everyMinute(){
if($this->input->is_cli_request()){
$count="i can count everyMinute".time();
echo " {$count}!".PHP_EOL;
file_put_contents("test.txt","Counted $count\n", FILE_APPEND);
}
}
(from our comments)
Make sure you put the complete path of the file. Crontab does need absolute references.
Related
I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.
*/5 * * * * php /var/www/html/cron/import-csv.php
Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).
I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/
There's a couple of ways I can think of off the top of my head:
Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)
And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)
SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php
One way is you can distribute your tasks is 3 different scripts and chain them in cron.
*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php
&& will make sure that, the next script runs only if the previous
ran sucessfully.
In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
Here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work.
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.
* is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
Specify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
EDIT 1:
To exit from vim editor without saving just click:
Shift+:
And then type q!
I had the same problem... I had to run it as a user.
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser (man runuser). Or create a system crontable and run the PHP script as an authorized user, as #Philip described.
I provide a detailed answer how to use cron in this stackoverflow post.
How to write a cron that will run a script every day at midnight?
I tried all combinations with PATHs, but don't work. Probably they are needed.
In my case, with Centos 7, a reboot or server worked.
I've got a PHP script that simply e-mails me a test message. If I go into my webserver cPanel I can create a cronjob that runs the script every 10 minutes and it works perfectly.
I manually schedule the cron job in cPanel using the following settings:
10 * * * * php -q /home1/user1/public_html/mail.php
Again the above works fine, but when I try to create the cron job via PHP instead of cPanel it never runs. When I check the cPanel to see if the job was actually created by my php script it DOES show up. All the settings that show up in cPanel are correct, it just doesn't run the script.
This is the PHP code I'm using to create the cron job:
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'10 * * * * php -q /home1/user1/public_html/mail.php'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
I imagine it could be a permission issue or something like that? Not really sure why the job works when I create it, but doesn't when PHP creates it.
PHP shell scripts, e.g. cli/cron jobs, STILL require a <?php tag. e.g.
#!/usr/bin/php
<?php
$output = blah blah blah
Remember that there's no such thing as a "php script". There's only files that have PHP code blocks within them. Without an opening <?php tag block SOMEWHERE in the file, the PHP execution engine will never kick in, even though the file will have been processed/parsed by PHP.
Without <?php, the file's contents will simply be treated as plaintext output.
Ended up figuring this out...I'm sure what was wrong with the php code I was using above but it seems that was somehow causing the execution failure.
I'm now using the code below and the job schedules and actually runs :)
exec('echo -e "`crontab -l`\n10 * * * * php -q /home1/user1/public_html/mail.php" | crontab -');
I'm having trouble setting up a cron job.
I have a cron job created at a url like so:
myurl.com/cron
When that link is accessed, it is run.
How do I set it so that it runs automatically, every 10 minutes?
I can't figure out how to create a cron job. I know that that's what I need to do, but I can't figure out how to do it for a ubuntu server.
Login via SSH and in the shell, type the following:
crontab -e
Then, place the following line:
*/10 * * * * /path/to/command
Save the file and that command will be called every 10 minutes.
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 :)