Run script daily with cron and create compressed log file - linux

Im trying to run PHP script on this location on every 15 minutes
0,5,10,15 * * * * /usr/local/bin/php /var/www/html/adform.php
and to put results of the script in specific folder in specific file and to compress it daily. Is it possible and how? Pls help.

This is possible.
Instead of running /usr/local/bin/php every 15 minutes, why not replace that call with a shell script which does everything you described?
This shell script would run your php script, pipe the output to a file somewhere on your system which you can then compress.

Related

PHP script via cron is not finding files called in the code

I want to run php script that is located on server at:
/var/www/my_directory/myfile.php
Script executes one function that uses certificate files located at:
/var/www/my_directory/my_cert_dir/cert_file.pem
inside PHP script they look like this:
$key="my_cert_dir/cert_file.pem";
I have also tried to add $_SERVER["DOCUMENT_ROOT"]:
$key=$_SERVER["DOCUMENT_ROOT"]."/my_cert_dir/cert_file.pem";
In both cases I get "file not found" for my certificate files.
However, when run through CLI or browser they execute without problems.
First I used cron like this:
13 19 * * * /usr/bin/php /var/www/my_directory/myfile.php >> /var/www/my_directory/my_logs_dir/some_log.txt
And cron executed the script and I got errors, so I tried to change cron to execute from directory like this:
13 19 * * * cd /var/www/my_directory; /usr/bin/php myfile.php >> /var/www/my_directory/my_logs_dir/some_log.txt
However this time I don't see that script executes and my log is not created.
Any ideas how to fix my issue so that script is executed and cert files are found?
Why not try the full path:
$key="/var/www/my_directory/my_cert_dir/cert_file.pem"

Ubuntu cron job

I wrote a bash script I saved it as a file getcalls.sh, I used chmod+x on the file to make it executable. It curls csv output from an api into a file in the same directory it runs in. It runs in a directory called data in one of my web folders. When I run it from command line it runs fine. When I try to put it in cron it doesn't run. My cron entry looks like this.
12 03 * * 3 cd /var/www/html/sitedir/data/getcalls.sh
Is the syntax incorrect?
Yes your syntax is incorrect. You must use it this way:
minute hour day-of-month month day-of-week command
But you provide cd command. Use it as follows:
12 03 * * 3 /var/www/html/sitedir/data/getcalls.sh

Cron job that saves file

Is it possible to run a cron job on host that also saves a file to your website directory? I have an API I download a xml file from hourly. I would like to schedule cron to automate this but I'm not sure if it's possible.
You can write a cron job to execute an arbitrary script. The script can do whatever you like provided it has the appropriate permissions.
* * * * * /bin/execute/this/script.sh
Will run the script script.sh
You need to configure the crontab entry appropriately (obviously).
Yes. You can schedule a script in cron.
In the script you can add lines to generate the file and to place the file in the location you want.
Note: Be sure that you have permissions to create a file in that specific location path.

Where to locate Centos 6 cron job .sh file

I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.

Specifying path to run php file in cron job

I have written a cron job in my Server as below
35 * * * * /home/sites/domain.com/public_html/admin/filename.php
I am trying to call a file in http://domain.com/admin/filename.php.
In Cron job i navigated to every folder by the way its stored in directory in server.But the way to reach the same file in browser is as below
http://domain.com/admin/filename.php
Now the cron filename.php is not running.I checked the file permission every thing is perfect.
I want to know is there a problem in the path i specified
Thanks in advance
When you run php file in cron. You must config as:
35 * * * * /usr/bin/php /home/sites/domain.com/public_html/admin/filename.php
and you want run daemon in server. You can setup as:
nohup php /home/sites/domain.com/public_html/admin/filename.php &

Resources