I have a PHP script creating a MySQL dump, working perfectly when launched from the browser. It calls mysqldump through the exec() function.
I scheduled this PHP script as root in the crontab (on an Ubuntu VPS). It fails with the following error message :
sh: 1: cannot create ./backups/db_20220522.sql: Directory nonexistent
I have set_include_path('/var/www/mysite.com/'); at the beginning of my PHP script. The script and the "backups" directory are in there.
What am I missing ?
Thanks a lot for your help
You need the specify the full path to your backup directory in your script .
Cron runs from a different directory.
eg
/home/username/domains/domain.com/public_html/backup
Related
I am trying to run a php script on a remote server using ansible.
Running the script with the ansible user (which ansible uses to login to the server) works perfectly. The ansible task however fails when there are include statements in my php script.
My php script lays in /srv/project
it tries to include includes/someLibrary.php
Everything works fine when running the script as any user with the correct access rights but when running it via an ansible task
- name: run script
shell: 'php /srv/project/script.php'
it fails with: failed to open stream: No such file or directory in /srv/project/includes/someLibrary.php
Running a very basic php script works nicely though.
I just found the solution to the problem.
The problem was that when I executed the script by hand, I connected to the server and cd'd into the /srv/project directory before calling php script.php PHPs include in this case looks in the current directory for the files I want to include. When ansible connects to the server it did not change the directory thus producing the no such file or directory error. The solution to this is simple as the shell module takes a chdir as an argument to change the directory to the one specified before running the command.
My ansible task now looks as follows:
- name: run script
shell: 'php /srv/project/script.php'
args:
chdir: '/srv/project'
Thanks everyone for your help!
Ansible runs under a non-interactive ssh session, and thus does not apply user environment settings (eg, .bashrc, .bash_profile). This is typically the cause of different behavior when running interactively vs. not. Check the difference between an interactive printenv and raw: printenv via Ansible, and you'll probably find what needs to be set (via an ansible task/play environment: block) to get things working.
I have 2 .php files in my application - book.php and weather.php. I create a file named "runscript" in /.openshift/cron/minutely. This file contents:
#!/bin/bash
php -f $OPENSHIFT_REPO_DIR/weather.php
This script send me message to phone every minute, it's OK.
Then I replace to:
php -f $OPENSHIFT_REPO_DIR/book.php
This script MUST send me message too, but nothing is happing. But if I just run this script by my webbrowser (go to the http://xxx-xxxxxxx.rhcloud.com/book.php) so I got my message. How is it possible? Magic?
Did you miss the #!/bin/bash part? That's needed to run the shell script.
For why your cron job is not executing, check the cron logs on OpenShift. You can find them at ~/app-root/logs/cron_*.log when you SSH into your gear.
Make sure your cron job is execuable with chmod, and has the shebang line as #gnaanaa says. Also check if you have one of the .openshift/cron/minutely/jobs.{allow,deny} files as they may cause cron to skip your job. (See the cron README for more information.)
And after your cron job is working, you can get rid of the wrapper script runscript and have cron call book.php directly. To do so, place book.php directly into .openshift/cron/minutely, make it executable, and add this shebang to it:
#!/usr/bin/env php
Hope this helps.
I use openshift aswell and executed a php file with a cron aswell.
#!/bin/bash
php ${OPENSHIFT_REPO_DIR}index.php
This executes the script normally at first sight. However no output was produced. The problem was, that all the required php files couldnt be loaded because the working directory was not the same as it would be when loaded by the webserver. Setting the working directoy in the php script itself will prevent this error and makes the script perfectly executable by the cron.
This should help some people to get their script running.
This is what i want to do in a batch file:
write a file to ftp folder
run php script (http://mylocation.url/script.php)
download a file from ftp folder
FTP i have read before, it is possible to do it in a batch file.
But i don't found a way to execute my php script on my linux server.
Cronjob is not possible for this solution cause the uploaded file will be changed from
Scipt and after i need it downloaded again.
Any one has a solution?
Thanks for help
Phil
plink tool can be used to execute commands in Linux server from Windows through SSH. Below is the syntax
plink.exe root#10.0.0.1 -pw password "<ur script execution command here>"
Say if I want to run a shell script I would type in the command "sh myscript.sh". To achieve this the Linux server should have sshd running.
The command can be used along with other commands part of the batch file.
Description: I am not very familiar with using a lot of bash/shell. I currently have a cron tab set up on an Ubuntu server that runs a Shell script. The Shell script then is suppose to run a PHP script, however, instead I am getting the following error message:
Could not open input file: wscript.php
At the top of my shell script I have written #!/bin/bash
Then the shell script itself I am passing a bash variable to PHP script.
while read bashvariable
do
php wscript.php "$bashvariable"
done
Keep in mind when I run this this shell script manually the script executes and fires correctly.
At the top of wscript.php I have placed in #!/usr/local/bin/php.
wscript.php has an include file of wscript-add.php
I have attempted to change the permission of all files to 777 and I haven't had any luck on getting the cron tab to run correctly.
Below is what my cron tab looks like:
*/2 * * * * sh /var/www/website/wcron/wcron.sh
My Question: What could cause my PHP file to not fire correctly when used by cron? Do I need specific file permissions on each file to run correctly?
You need to specify full path to php script as when cron runs, it uses different current directory.
I have a php script that connects to a database and does inserts/updates, obviously, if I run the script using the browser, it executes successfully, moving it into production this won't happen.
I would like to ask how can I invoke the php.exe using a batch script that then runs the php script I have created to do the inserts/updates.
I have the ff: setup
I'm using xampp, with the ff:
webdirectory/cronjobs/cronjob.php -> does the inserts and updates
webdirectory/cronjobs/runscript.bat ->will run the cronjob.php
runscript.bat
#echo off
php.exe -f ../webdirectory/cronjobs/cronjob.php
cronjob.php
include_once('../db/dbcon.php');
$test = $db->run();
.... insert/update codes
Additionally, when I try to run the batch script directly using command line for Win7, using drive:>/xampp/php/php.exe -f drive:>xampp/htdocs/webdirectory/cronjobs/cronjob.php
I get the ff: error;
Warning: include_once(): Failed opening '../webdirectory/db/dbcon.php' for inclusion (include_path='.; E:\xampp\php\PEAR') in E:\xammp\htdocs\webdirectory\db\dbcon.php on line xx
I guess the relative file path is failing; Follow-up question tho, does that mean I have to edit my script file and set the path to absolute? Instead of using ../webdirectory/db/dbcon.php , I should use e:/xampp/htdocs/webdirectory/db/dbcon.php ? Looks like a bad thing to me...
Can somebody please help?
change the directory to the location of the script to make all relative paths work when beeing executed from outside the script's directory
cronjob.php
chdir(__DIR__);
include_once('../db/dbcon.php');