not calling php script from shell script which installed in crontab - php

i have move_files.sh and installed it in crontab.
Actually job is working because it's printing those echo. And creating log file.
But it's not calling that PHP script.
Interesting thing is if i run it by manually it's calling php script and working 100%. But why it's not calling after i installed it on crontab.
should i put "php" before calling php script. I am thinking that cronjob would work same as manually running script. Please give me idea.
My code is below.
#!/usr/local/bin/bash
DIR=/data/aa/bb
LOG=~account/HOME/log/dd
DATE=`date +%Y%m%d`
LOG_FILE=$LOG/move_files.$DATE.log
PROG=~account/HOME/bin/move_files.php
for type in "1" "2" "3"
do
echo "Check files in $DIR/dat/$type" >> $LOG_FILE
$PROG $DIR/dat/$type $DIR/backup/$type >> $LOG_FILE
echo "Compress files in $type" >> $LOG_FILE
find $DIR/backup -name "*.DAT" -type f -exec gzip -f {} \; >> $LOG_FILE
done

I had the similar issue as you're doing. My backup script was working very well if it's called directly but wasn't working in crontab at all. I figured it out by adding this in the start (after shebang) of bash script:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt
Where "/opt" in the last is the directory where my bash script exists.

Try calling the script specifying php binary path.
/path/to/php_binary script_file.php
E.g:
/usr/bin/php5 myscript.php argument1 argument2 >> mylogfile.log

If you execute the script locally, you will visit to that folder (i.e. ~account/HOME/bin/)
By executing cron, it will use a new shell to execute the *.sh file in the present path. Therefore what you missing is to visit the path of where the .php/.sh file is contained. (absolute path is recommended)
cd ~account/HOME/bin/
The log file can be created because you issue an environment variable $LOG which indicates the exact location of that log file.

Thanks Guys,
anyway i put on myscript following line:
export PATH=$PATH:/usr/local/bin
Then it's working now.
Thank you all of you guys.

Related

Execute PHP routines on Windows Server [duplicate]

How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
It just opens the PHP file in Notepad.
I gave the correct user name and pwd.
Please help me..
The Run command should be
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
-f Parse and execute <file>.
I just wrote a .bat file that does the work file.bat
#ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
Run: C:\code\file.bat
Start in: C:\code\
If you running php scripts, in most of cases script awaiting to be runned in current folder.
That mean you must set up folder each your action, use field "Start In".
Example:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
Start in: C:\web\
Sounds like you don't have PHP files associated with the EXE.
You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.
Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.
This is what I did:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
#ECHO OFF
php -f d:\wamp\www\V3\task.php
This site might help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
For those people that can't able to make #Tomalak's answer work:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.php if there are included files in it.
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.
I think, you must execute your PHP script via URL.
you can write batch script for execute URL.
Why you don't write backend script in other language such as batch script, vbscript or etc.

php script to run bash script which runs scrapy crawler

I have a form in html page that has action to run php file. The php file has to run a bash function command which in turn runs a scrapy spider. Since I have the scrapy spider located outside var/www/, I have added a function in .bashrc to run the bash command ($ startscript) from anywhere. When I run it on the terminal from var/www folder or anywhere it works as expected but when I do it in php file it does not work. I am not sure if its because of php file permission, scrapy proprieties or something else.
Any suggestions?
.php file:
<?php
$output = shell_exec('startscript');
echo $output;
?>
.bashrc file:
function startscript
{
cd /home/pi/IndeedCoUkCrawl
./BotScrapy.sh
}
So what you can try:
put the absolute path of the startscript in shell_exec and see if it works.
try running with error_reporting(E_ALL) and see if this give any error/notice
check if your webserver has permissions to run the file. Try putting the file into the same group as the webserver runs in.

Few questions about SUID, php exec() and linux config files

What SUID\SGUID I need to add for sh script for root executable of it?
I want to see working script with root rights from php exec function.
exec('cd /usr/share/htvcenter/local-server/bin/ && ./panelhost-local-server-nfsip >> log.txt');
What I must to add to config file as well for see another file like config?
I want to see /etc/exports_add like continue of config /etc/exports is it possible?

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

Problem executing bash file

HI there!
I've run into some problem while learning to combine .sh files and PHP. I've create a file test.sh and in that file I call a PHP file called test.php.
If I double click on the .sh file then it runs perfectly but when I try to run it from the terminal I get "command not found". I'm in the exact folder as my .sh file but it wont work. Here's my test.sh:
#!/bin/bash
LIB=${0/%cli/}
exec php -q ${LIB}test.php one two three
exit;
When I doubleclick on the test.sh file then it returns the argv array like it suppost to. But why can't I run it from terminal?
use ./filename.sh
no matter if your are in the same folder or not, without giving ./ the system is searching the path variable. (/bin/, /usr/bin and so on)
Is execute bit enabled?
chmod +x test.sh
Your $PATH variable may not include '.' - so the shell may not be able to find the command to run.
As others have said, sh ./file.sh will work...
It is possible that your environment is different when launching from the Terminal and when launching via a double-click. Try executing which php and echo $PATH from the Terminal to see what the deal is.
EDIT 1
As others have noted, if your "command not found" is referring to the shell script and not php, then you probably forgot to include the "./" before the name of the script (i.e. ./test.sh). Also, don't forget to make it executable by invoking chmod a+x test.sh. The reason for this is that PATH does not include the current directory (i.e. "."), because doing so would be a security risk (e.g. folders with a ton of files in them including a fake "ssh" which could then intercept your password or the like).
EDIT 2
Also, I don't know about you, but ${0/%cli/} is evaluating to -bash from within my Terminal. Are you sure that's what you wanted it to expand to? Perhaps you should specify the exact filename.
Another option is to run it with sh (or bash, if sh on your machine isn't bash and the script uses bashims)
sh filename.sh
bash filename.sh
This will work whether or not the file is executable or in $PATH

Categories