Integrating php in shell scripts for a cronjob? - php

I would like to execute a cronjob for a routine task every X hours. The cronjob basically executes a shell script which in turn uses a WGET command to download files from a remote server. However, before I run this shell script I want the cronjob to execute a php script which will check whether the update's available (there's no point in wasting BW and downloading the same file over and over again) and if it is, it should pass on the update URL to the shell script which in turn uses the WGET command.
The cronjobs are set from the hosts Admin Panel. There is no other way around it. Being a shared hosting service, I am not allowed access to other functions on PHP which might do the task for me either.
Is this possible? I am Linux illiterate. I have installed a few RPM's on Fedora but that's about it. Please bear with me. Thanks!

Just pass --timestamping to your wget command.
Alternatively if you are more familiar with PHP's ways you can check this question for a usable method.
Use a curl HEAD request to get the file's headers and parse out the Last-Modified: header.
To use a php script as a regular command line executable use this as a starting point:
#!/bin/env php
<?php
echo "Hello World\n";
Save the file without the .php and tuck it somewhere that your server won't serve it.
Next, set the executable bit so that you can execute the script like a regular program
(u+x in the following command means grant the [u]ser e[x]ecute privileges for helloworld, and chmod is the command that unix variants use to set file permissions)
Omit the $ in the following sequence, as it represents the command prompt
$ chmod u+x helloworld
now you can execute your commandline script by calling it in the bash prompt:
$ ls
helloworld
$ ./helloworld
Hello World
$
From here you can get the full path of the executable script:
$ readlink -f helloworld
/home/SPI/helloworld
And now you can install the cronjob using the path to your executable script.

Related

PHP exec() and command line switches

So I am using PHP exec() to run a batch file on my server:
echo exec("printCountries.bat");
The batch file opens Microsoft Access 2007 and runs a macro to print a form and then close Access:
START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
I can run the batch file from the command line successfully and the form prints to our network printer (this is for a company intranet) and closes Access. But when I run the PHP script, the batch file only runs the Access Process but does not perform the command line switches or open the database file.
I have configured PHP to have the right privileges on IIS, I am just stumped as to why the command line switches don't work and the batch file won't open the database.
Any ideas?
here is the echo output:
C:\inetpub\wwwroot\scale>START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
It looks like you have some access rights problem.
Did you try to run in PHP this echo exec("whoami");
What username you get?
Something like "nt authority\iusr" ?
You have to give to the default IIS user (i.e. IIS_IUSRS) full access to the path that your scripts lives.
In addition you can change (only TEMPORARILY huh?) the default application pool user from ApplicationPoolIdentity to LocalService or LocalSystem to test the results...
If I were you, I would created a new Application in IIS then I would created a new Application Pool and I would assigned it to this app. Then I created a new user that will have access to all the appropriate directories (web dir and where the bat is located).
If nothing of the above worked I would test my php application from my command line (locally) using the php command in my local command prompt.
I am neither good with PHP, nor with Access. However, when you invoke the batch file from PHP, please create an instance of cmd.exe and pass "/c printCountries.bat" to cmd.exe. In other words, the command run by PHP exec should look something like this (assuming PATH is set correctly):
cmd.exe /c printCountries.bat
Also, inside printContries.bat, I think you will need to wait until the access process terminates. So it should look like
START /WAIT /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries

perl script is not running through cron

I need your help here.
I wrote one PERL script for PHP application which needs to be run for every 5 mins.
This script will call PHP program, which will fetch data from MySQL DB and will generate a excel report and will mail those reports to specific users.
Every thing seems to be fine when I ran this script manually with the command (perl reports.pl).
But when I set this Perl in a cron tab, nothing works and reports are not getting generated.
Details: perl script path /opt/app/deweb/web/EDI/Microsoft/reports.pl
this script will call PHP program (/opt/app/deweb/web/EDI/Microsoft/reports.php)
content of script
#!/usr/local/bin/perl
use Net::FTP;
use File::Copy;
use POSIX;
#errorreport = `php /opt/app/deweb/web/EDI/Microsoft/reports.php`;
print "#errorreport\n";
exit;
It is working perfectly when running Manually using command - perl reports.pl
No results, when set in CRON:
*/5 7-19 * * * /usr/local/bin/perl /opt/app/deweb/web/EDI/Microsoft/reports.pl
Please note that this crontab is under super user account named webserv and my login is having access to edit under this super user account.
I'm editing this cron tab using command :: sudo -u webserv crontab -e
I would check the following:
Does it run using sudo -u webserv perl reports.pl? If not, fix the problem for the webserv user (permissions or whatever) and it should work via cron too.
Does which perl using your login give you /usr/local/bin/perl? If not, change the path to Perl in crontab to what you got in which perl to fix the problem.
I found myself to be in the same situtation. After trying to find out the reason, I am almost sure about the reason this happens. Crontab does not have the same environment variables as you when running the script. You must be sure about paths. Try for example run your script like /perl-path /path-to-perl-script/script.pl outside the parent directory of the script and I am almost sure that your programm will not find some files. And as you call one php script from the perl script, it's possible to have the same problem with paths to your php script too.
So the solution is to use absolute paths and no relative.
Also at your perl script don't use php but /full-path-to-php for example:
#errorreport = /usr/bin/php /opt/app/deweb/web/EDI/Microsoft/reports.php;

Unable to setup cron-job on Amazon EC2

I have an Amazon EC2 instance (Ubuntu Server 13.04 - 64 bit [ami-bf1d8a8f]) running my website. I need to setup a Cron Job to get an email alert everyday.
Does anyone have any advise or possible solutions?
Thanks for your time.
It's just the same as setting up a cron job on any other server via command line.
Connect via SSH
Navigate to /etc/cron.daily
Make a new script that runs / calls a PHP script to send the email/other tasks
Make sure its executable
You can use a command such as wget -q -O temp.txt http://www.site.com/cron.php to call the PHP script, or via command line php /var/www/site/cron.php.
Regarding the wget method, temp.txt will contain the output of the script, or you can redirect the output to /dev/null. This will just discard all output. If you need to save the output of the cron script and you decided to go with the command line method then you can output the data to a file php /var/www/site/cron.php > output.txt
If you need more explanation/detail post a comment. Also take a look at Basic cron job set up to execute php file daily
Step 1: Create a bash script in the cron.daily folder
Connect to the instance via SSH, navigate to the daily cron folder with cd /etc/cron.daily. Next type sudo nano mailscript, notice there is no .sh bash extension. I had a problem with the extension which caused the script to not run.
Step 2: Bash script contents
Enter this into the command line text editor nano
#!/bin/bash
php /var/www/mail-script.php > /var/www/mail-script-log.txt
Now save the text file and exit, to save use CTRL + O and then press enter, to exit press CTRL + X.
Step 3: Make it executable
We need to allow the file to be executed, type sudo chmod +x mailscript.
Step 4: Try it out
To test it out type ./mailscript to run the cron in cron.daily. Now check your emails!

PHP shell_exec() issue

I am having an issue using the PHP function shell_exec().
I have an application which I can run from the linux command line perfectly fine. The application takes several hours to run, so I am trying to spawn a new instance using shell_exec() to manage better. However, when I run the exact same command (which works on the command line) through shell_exec(), it returns an empty string, and it doesn't look like any new processes were started. Plus it completes almost instantly. shell_exec() is suppose to wait until the command has finished correct?
I have also tried variations of exec() with the same outcome.
Does anyone have any idea what could be going on here?
There are no symbolic links or anything funky in the command: just the path to the application and a few command line parametes.
Some thing with you env
See output of env from cli (command line interface) and php script
Also see what your shell interpreter?
And does script and cli application runs from one user?
If so, se option safe_mode
Make sure the user apache is running on (probably www-data) has access to the files and that they are executable (ls -la). A simple chmod 777 [filename] would fix that.
By default PHP will timeout after 30 sec. You can disable the limit like this:
<?php
set_time_limit(0);
?>
Edit:
Also consider this: http://www.rabbitmq.com/

how to call php script from just server by bash?

i want to execute a php script every day. i have a bash script containing below lines. and i was doing it with that script. but by this way, it can be executed from another computer. lets say webservername="slmnbr" any body can call myscript with
xhttp://slmnbr/myscript.php.
i want to call it from just server.
BASE_URL=http://`get-webservername`
/usr/bin/wget --no-check-certificate --timeout=0 -O - "$BASE_URL/myscript.php"
thanks in advance
You need to have PHP CLI (command line interface) to do this. It often comes with the PHP installation package (or a separate package).
So as #James mentioned it you need to precede you script name with the CLI PHP executable which in windows is php.exe somewhere in your install directory.
In UN*X systems it's somewhere in /usr/bin or /usr/local/bin or /opt/local/bin. If it is in the PATH you can simply execute it like this:
php your_script.php
But keep in mind that running your script from server will be not the same as running from a webserver. For e.g. you won't have the POST or GET variables. So your script may not work.
Update
You can make the cron ran scripts unreachable for your webserver by making it unreadable by the user who is running the PHP scripts by the http daemon (usually www-data).
I recommend to make it readable only by the user who is running it from cron:
chown cronuser your_script.php
chmod 0400 your_script.php
Update2
If you're running your script in cron with the same user as the webserver does, then at the top of your script stop it from being executed:
if (!(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))) {
header('HTTP/1.1 403 Forbidden');
exit;
}
To execute a task(or a php script) every day use cron.
The link has all the info you need, including how to prevent external access.
With any recent version of "php" you should get a standalone executable called:
"php.exe"
Just execute this like so:
/usr/local/php.exe your_script.php

Categories