can I run a rake task from php?
I tried shell_exec but nothing happens.
exec('/bin/bash -l -c \'cd /Users/username/www/rails_app && [[ -s "/Users/username/.rvm/scripts/rvm" ]] && source "/Users/username/.rvm/scripts/rvm/.rvm/scripts/rvm" && rvm use 2.1.2 && RAILS_ENV=development /usr/bin/rake "ko:complete_order[2]" --silent\'', $out, $err);
The $out is an empty array.
The $err is 1.
Related
been trying to figure this one out for a couple of hours now. No luck.
I am trying to build a system that can run reports (running scripts in the background) using shell_exec.
The following code starts the script that runs the report:
shell_exec("php /var/www/html/lab-40/test/invoice_reminder.php");
Now how would I go about ending that script execution using PHP?
I've tried things like PIDS but I have no clue how I would go about this. Thanks for the help in advance!
EDIT: I am not trying to end the process if the e.g tab is closed.
Based on this comment on shell_exec help page (& will bring the process to the background, and echo $! will print the process PID):
<?php
function shell_exec_background(string $command): int {
return (int)shell_exec(
sprintf(
'nohup %s 1> /dev/null 2> /dev/null & echo $!',
$command
)
);
}
function is_pid_running(int $pid): bool {
exec(
sprintf(
'kill -0 %d 1> /dev/null 2> /dev/null',
$pid
),
$output,
$exit_code
);
return $exit_code === 0;
}
function kill_pid(int $pid): bool {
exec(
sprintf(
'kill -KILL %d 1> /dev/null 2> /dev/null',
$pid
),
$output,
$exit_code
);
return $exit_code === 0;
}
$pid = shell_exec_background('php /var/www/html/lab-40/test/invoice_reminder.php');
var_dump($pid);
var_dump(is_pid_running($pid));
var_dump(kill_pid($pid));
I'm using the PHP exec() function to execute the Canu assembler programs, and I want to get its process ID within the same script.
The problem is exec() not returning any PID, even the process is running successfully.
The processes are started like this:
$gnuplot_path = '/usr/bin/gnuplot';
$command = 'nohup canu -d . -p E.coli gnuplot='.$gnuplot_path.' genomeSize=4.8m useGrid=false maxThreads=30 -pacbio-raw /path/to/p6.25x.fastq > /path/to/process.err 2>&1 &';
Currently, I try to determine if the process is still running by:
$pid = exec($command, $output);
var_dump($pid);
and also this:
exec($command, $pid, $return_var);
print_r($pid);
echo "$return_var\n";
However, I got output of string(0) "" and Array ( ) 0 respectively.
Please let me know how to solve this. Thanks much.
This one is tricky. What I would do:
$gnuplot_path = '/usr/bin/gnuplot';
$command = 'nohup canu -d . -p E.coli gnuplot='.$gnuplot_path.' genomeSize=4.8m useGrid=false maxThreads=30 -pacbio-raw /path/to/p6.25x.fastq > /path/to/process.err 2>&1';
$command .= ' & echo $!';
$pid = exec($command, $output, $a);
var_dump($output[0]);
I have 3 scripts (I have removed the help_page function from the networkstats.sh script when I pasted here to save some space):
api3.php
<?php
output = shell_exec('/bin/bash /usr/share/nginx/status/getnetworkstatsin.sh');
echo $output;
?>
getnetworkstatsin.sh
#!/bin/bash
ssh -i /tmp/id_rsa1 root#centos7clone bash -s -- -I < ./networkstats.sh
networkstats.sh
#!/bin/bash
interface=enp0s3
read -r inbytesold outbytesold < <(awk -v dev="^$interface:" '$1 ~ dev {
sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)
sleep 1
read -r inbytesnew outbytesnew < <(awk -v dev="^$interface:" '$1 ~ dev {
sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)
kilobitsin=$(( ( ( inbytesnew - inbytesold ) * 8 ) / 1024 ))
kilobitsout=$(( ( ( outbytesnew - outbytesold ) * 8 ) / 1024 ))
show_outgoing() {
echo $kilobitsout
}
show_all() {
echo "kilobits in: $kilobitsin"
echo "kilobits out: $kilobitsout"
}
if [[ $# -eq 0 ]];
then
help_page
exit 1
fi
for arg in "$#"
do
case $arg in
-h|--help)
help_page
;;
-I)
show_incoming
;;
-O)
show_outgoing
;;
-A|--all)
show_all
;;
esac
done
The problem I have is that when I execute the api3.php script from console, it is able to execute and return a value.
However when I try and execute from a webpage it fails to return anything.
I believe it is not even executing when I load it via the webpage by navigating to localhost/api3.php. Can someone help, what is the reason behind this? I have added
nginx ALL=NOPASSWD: /usr/share/nginx/status/getnetworkstatsin.sh
To my visudo section, I have tried to change permissions of all files involved to 777 (temporally) without success.
EDIT: I should also mention that all these scripts are located inside /usr/share/nginx/status which nginx has access to.
I can't find the error, the scripts runs fine from console, I thought it was PHP thing but couldn't fine anything.
Maybe permissions things? Exec things? I really don't know much, hope someone can help me. Thanks!
BASH Script (SIMPLY SENDS A JOB TO A PRINTER, but I put it enterely just in case):
#!/bin/bash
PBOX_DIR_TMP="/tmp"
DESDE=$1
HASTA=$2
FORMULARIO=$3
COLA=$4
FECHA=$(date +%F)
SPOOL="/spool.$$.txt"
ARCHIVOSALIDA="${PBOX_DIR_TMP}"/salida.$$.txt
RETURNCODE=0
echo "$DESDE"
echo "$HASTA"
echo "#PBSSFORM ${FORMULARIO}" > "${SPOOL}"
for ((i = ${DESDE};i <= ${HASTA};i++))
do
if [ $i > ${DESDE} ]
then
echo -e "\f${FECHA}" >> "${SPOOL}"
echo "${i}" >> "${SPOOL}"
else
echo "${FECHA}" >> "${SPOOL}"
echo "${i}" >> "${SPOOL}"
fi
done
cat "${SPOOL}" | pboxsvc ${PBOX_DIR_BIN}/pboxlib.bin AplicarHostForm > "${ARCHIVOSALIDA}"
lp -d "${COLA}" "${ARCHIVOSALIDA}"
RETURNCODE=$?
rm "${ARCHIVOSALIDA}"
rm "${SPOOL}"
if [ ${RETURNCODE} -eq 0 ]
then
exit 1
else
exit 0
fi
PHP call:
$cmd = "/printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1";
Apache Log:
/printb/imprimirFormPlano.bin 1: Syntax error: ")" unexpected
sh: 2: not found.
The scripts works if I call it from shell like this: ./printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1
It looks like your apache's default shell is sh rather than bash. Try changing your command to
$cmd = '/bin/bash /printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1';
I'm a noob, is there someone could help me with this?
I have a local SQL database using Xampp thru phpMyAdmin and i want to mirror it to online MySQL in my website.
When I'm making changes to my local database, I want my online database to be updated (Realtime) with the changes made.
Is this possible? Can I have a sample php code or whatever that can do this? Thanks!!
Many times ago I wrote some script for this problem. As said above - replication it's really good solution but in my case I could not use it. So.. if you need some like replication by master->slave maybe this script will be useffull :
dumps.sh :
while getopts "c:l:" opt; do
case $opt in
c)
if [ -r "$OPTARG" ]; then
source "$OPTARG"
else
echo "Unreadable config file \"$OPTARG\""
exit 1
fi
;;
l) LOG_FILE="$OPTARG"
if [ ! -f "$LOG_FILE" ]; then
`touch $LOG_FILE`
fi
;;
\?) echo "Invalid options. -$OPTARG. USE -c config_file"
exit 1
;;
:) "Option -$OPTARG requires an argument."
;;
esac
done
logIt()
{
date_now=`date '+%D %T'`
if [ $LOG_FILE != "" ]; then
echo "$date_now : $*" >> $LOG_FILE
else
echo "$date_now : $*"
fi
}
build_tables()
{
TAB=""
logIt $#
for table in $TABLES
do
TAB="$TAB ${1}${table}"
done
echo $TAB
}
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
RM="$(which rm)"
DEST="."
MBD="$DEST/mysql"
eval $RM -fv "$MBD/*"
FILE=""
[ ! -d $MBD ] && mkdir -p $MBD || :
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
TAB=`build_tables $DB_PREFIX`
FILE="$MBD/$DB_NAME.sql";
($MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $DB_NAME $TAB 2>> $LOG_FILE) > $FILE
input_to_mysql()
{
###############################3
CP="$(which cp)"
SED="$(which sed)"
len=${#INSERT_DB_NAME[*]}
i=0
while [ $i -lt $len ]; do
NEW_FILE="$MBD/${INSERT_DB_NAME[$i]}.sql"
eval $CP $FILE $NEW_FILE
eval $SED -i "s/$DB_PREFIX/${INSERT_DB_PREFIX[$i]}/g" $NEW_FILE
eval $SED -i "s/^.\*!.*$//g" $NEW_FILE
let i++
done
i=0
while [ $i -lt $len ]; do
NAME="$MBD/${INSERT_DB_NAME[$i]}.sql"
if [ -e $NAME ]; then
$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME 2>> $LOG_FILE
#echo "$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME"
logIt "IMPORT TO ${INSERT_DB_NAME[$i]}"
else
logIt "File $NAME not exist";
fi
let i++
done
}
check_dump()
{
FILE_TMP_DUMP="$MBD/tmp_dump_${INSERT_DB_NAME[0]}.sql";
FILE_DIFF_RESS="$MBD/diff_res.diff"
tmp_tables=`build_tables ${INSERT_DB_PREFIX[0]}`
($MYSQLDUMP -u ${INSERT_DB_USER[0]} -h ${INSERT_DB_HOST[0]} -p${INSERT_DB_PASS[0]} ${INSERT_DB_NAME[0]} $tmp_tables 2>>$LOG_FILE) > $FILE_TMP_DUMP
DIFF="$(which diff)"
$DIFF $FILE $FILE_TMP_DUMP > FILE_DIFF_RESS
[ -s "$SMB/diff_res.diff" ];
SUCCESS=$?
eval $RM -f $FILE_TMP_DUMP $FILE_DIFF_RESS
return $SUCCESS
}
if check_dump; then
input_to_mysql
else
logIt "No need to dump"
fi
so run like dump.sh -c config -l log.file
Where config like this:
MyHOST="master_host"
MyUSER="master_user"
MyPASS="master_password"
DB_NAME="master_db_name"
DB_PREFIX="master_db_prefix_" # leave empty if you haven't table prefix
TABLES="table1 table2 table3" // list of tables - leave empty for all tables
###############################
INSERT_DB_NAME=(slave_dbname1 slave_dbname2)
INSERT_DB_HOST=(slave_host1 slave_host2)
INSERT_DB_USER=(slave_user1 slave_user2)
INSERT_DB_PASS=(slave_pass1 slave_pass2)
INSERT_DB_PREFIX=(slave_db_prefix1 slave_db_prefix1) //
I putted it to cron and all works fine for me.
Of course you can do all this manually ...
The word you are looking for, is Replication. It's not real time, but almost.