I'm trying run exec with mysqldump in my PHP file. It doesn't work, I'm even trying to use it without variables like this:
exec('mysqldump --user=xx --password=xx --host=xx db settings > /output/dump5.sql');
It creates empty file.
But when I run this command in ssh, it works. Why?
Do you need to do this from php script ?
I assume that you want to do the backup frequently, so maybe adding it to cron job will better solution.
Related
I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual
I want to set a cron job using PHP file, but don't know where the problem is coming in my process.
When I wrote below (in PHP file and executed it on browser), it shows me the result. (It lists all cron jobs)
echo shell_exec('crontab -l');
But when I wrote below line, then it didn't set any cron job:
echo shell_exec('crontab /home/testsite/public_html/crons/crons.txt');
But If I run the same command (crontab /home/testsite/public_html/crons/crons.txt) via puTTY on my CentOS Dedicated Server, then it updates the crons list from crons.txt file.
I also tried passing -u as I saw on several stackoverflow questions:
echo shell_exec('crontab -u testsite /home/testsite/public_html/crons/crons.txt');
Can anyone help?
I'm not sure what exactly you are trying to do, and the way you are trying to change the crontab is not the best way either. Let me show you how I would do it:
First, get the user you are when executing the PHP by putting into the PHP script:
echo get_current_user();
Depending on the cron software you use (I use anacron, like most other ppl I guess), you go the cron config directory and put a single file for you up with write access:
touch /etc/cron.d/testsite
chown <user from above>:<user(=group) from above> /etc/cron.d/testsite
With PHP, you can directly read and write to that file now (file_get_contents(); file_put_contents()) and crontab will immediately use it.
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;
I want to be able to dump a whole sqlite database to a file (database.sql) so as to use it as a backup. The problem is that exec() in PHP doesn't seem to run correctly the ".dump" command as it gives only "COMMIT;" as a result and not the whole text:
$db = new SQLite3('checks_db.db');
$results=$db->exec('.dump');
$content=$results->fetchArray();
If I run it from sqlite3 it gives both the schema and the insert insrtuctions with the data.
Perhaps I should also mention that the database has 777 permissions.
Can anyone write an example of the syntax of the '.dump' command in php-sqlite3?
The SQLite database does not have a .dump command.
The sqlite3 command-line shell has a .dump command.
To be able to use it, you would have to execute that tool; something like this:
exec('sqlite3 /some/where/checks_db.db .dump', $output);
The easiest way to make a backup would be to copy the database file itself, but using the shell's .backup command would be safer.
Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig