I am in a bit of a situation where I need to manually restart queues on linux/ubuntu based box and the proper approach I was told to follow is to use the screen command line application.
I am on Ubuntu 14.X and when I do an -ls command I can actually see the sessions running on the server. Below is a command I would run if I was outside screen session:
mycommandline$ php artisan queue:restart
Now the issue is that if from the -ls output I grab the session's port to include in the command I run I get the error below:
Cannot exec 'php artisan queue': No such file or directory
I also tried typing screen "php artisan queue:restart" and I keep getting the same issue.
I am not farmiliar at all with the screen program and any suggestions to point me to the right direction would be much appreciated.
Okay digging through google I came up with the command below. Given that I already had a session that was running I needed to make sure that I send my commands on that same session just to be sure. Below is what I ended up doing which does confirm that something is happenning within the screen session:
luyanda#devbox:~$ screen -d -r 19167 -p0 -x stuff "free -g; php artisan queue:restart"
Attaching from inside of screen?
As I am not seeing any visible issues I am convinced that this is what I needed to do given that my log file is updating as well it either means that the queues never stopped working or I managed to restart them.
What I am still interested to know is how I can actually see the output of the commands I send to my session else if I can rather pipe the output somewhere else.
Any thoughts?
Related
I need to run a PHP script which is scraping a website for data, and I need to run it on my VPS which has linux. I want to run it as a task so that I should be able to logout of my VPS and the script should keep running in background.
I've read about CRON job but it's more for like scheduling and repetitive tasks; but I need the PHP script only once.
Is there a way in PHP to do that? Please help, I'm just a newbie to this.
Thanks in advance! :)
I've tried it as a CRON job, but it doesn't seem to serve my exact purpose.
so I run my script like this from terminal.
php scrapethewebsite.php
and then it show this
Started scraping at 10:03:00 20-03-2019
and I can't logout or close my vps/ssh connection.
I look for
php scrapethewebsite.php
Started scraping at 10:03:00 20-03-2019
and then I should be able to logout or close my connection. And then I should be able to shutdown my PC and go for a walk..
Yes, You can do this with the screen. The screen is most of the time already installed in Linux VPS. but still, you can get by command.
apt-get install screen
Its give you the ability to have multiple screens in VPS where you can run multiple tasks at the same time.
LIke you have.
Get a screen with command.
screen -S sessionname
sessionname will be ur screen name.
and you can dispach it with command/
CTRL + A, followed by D.
then you can close your putty or any tool from via you accessing your vps.
here you go.
for more information, you can follow this link.
Screen in Linux
Run this in your SSH session, then you can click X and close it and it will still run
nohup php scrapethewebsite.php >/dev/null 2>&1 &
to check if your file is running type this command
top
you should see the php file up there and press spacebar to update the list
_____________________________________________________________________
If your php script has some error from time to time or just runs out of execution time and you want to re-run it when it closes down.. you have to create a .sh file with a while loop inside it and run nohup on it so it will re-run the php file after it errors up.
If you want to use nohup on php file that needs to re-run from time to time, then you should do the following
$ echo 'while true ; do php scrapethewebsite.php ; done > /dev/null' > ~/php_run_loop.sh
$ chmod a+x ~/php_run_loop.sh
$ nohup ~/php_run_loop.sh
NOTE: If you have an error or other problem in your php script, this will cause it to run over and over again without any limit, forever.
YOLO: https://github.com/pjreddie/darknet
I'm trying to run this from within a php script, but predictions.png never appears. I assume this is a general problem with my understanding of how exec works, rather than how YOLO works.
I've changed the permissions and users on all relevant folders and directories to nginx/777. I know that's not ideal but it's a troubleshooting step!
What happens when calling darknet from the terminal:
Logging output appears on the screen, and at the end of the process a file called predictions.php should be created in the /darknet directory. When running this from PHP in the browser I don't care about the logging output, I just want the png file to be created.
Works:
/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg
Works:
sudo -u nginx php71 -r 'exec("/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg");'
Works from commandline (php71 myfile.php), but not in the browser:
<?php
exec("/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg");
What am I doing wrong?
One of my lines of shell command is not executing despite other similar lines working. I am running on a linux machine using a Ubuntu 12.04 based OS. I have tried using exec as well, still doesn't work.
I actually had this working at some point, where I ran into the hanging issue (waiting for command output), which is why I'm redirecting output to /dev/null. So some where in the development something changed. We did create a debian package to install with and I had run that install package so I thought maybe in overwriting a file the permissions got changed so I added read/write/execute to all users/groups/owners but that didn't work either.
The code is here:
if(isset($_POST['activateXML']))
{
if (videoConsistencyCheck())
{
`cp {$fileXML} /apps/video/xml.xml`;
`sudo /apps/video/vsss restart >/dev/null 2>&1 &`;
systemUnvalidate();
header('Location: index.php?app='.$_GET['app']);
die();
}
}
I know that the first line in the if statement gets executed. The line of code works fine in the actually terminal, so that isn't the problem either. I did lots of Googling and all I could find is an unanswered question, any advice would be helpful.
EDIT: so what appeared to be not working was in fact calling the command as intended but in the bash script I was calling the start-stop daemon was not working
EDIT 2: I made a test php file and ran the code from the terminal, fixed the start-stop-daemon error by adding sudo to the commands but it still doesn't work in my code. I am calling this code when a submit button is pressed.
use additional parameters, especially output:
exec($command,$output);
var_dump($output);
to determine what can be wrong with your command. If it doesn't work, please show us your code where you use your exec's.
The issue lay with a call to a binary file in the vsss script that could only be run as root. We did not want to allow access to that binary file to just anyone. The solution we came up with involves calling chmod +s on the vsss script which allows permissions for user and group IDs but keeps its owner permissions. We then added the PHP user, which was www-data, to the sudoers file using the NOPASSWD parameter. In my PHP code I then used the line:
exec('sudo /apps/video/vsss restart >/dev/null 2>&1 &')
The shell_exec()/backticks would not work with this method.
I am trying to run a php file in my web server in command line using
php test.php
But it is not working. it runs forever and takes 100% resources.
It was working yesterday and now it is not working.
I tried to debug the process using
strace -p <pid>
and got lots of
times(NULL) = -2058427839
Can anyone help me to debug this? Rebooting and upgrading is my last resort. Please suggest a solution without reboot or upgrade.
Try to run your php file in cli without the configuration file using the following command:
php -n <your-filename.php>
and then check whether its working?
Place this into your test.php file to check that it is working and to see what is available.
phpinfo();
I am attempting to create a php script that can connect thru ssh to my Qnap TS219 server and run a command on it.
My script so far connects fine to the server but when I run the command I get an error message and I can't figure it out.
exec.sh
#!/bin/bash
cp /share/MD0_DATA/Qdownload/rapidshare/admin/script.txt /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh
chmod 755 /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh
nohup sh /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh &
exit 0
script.sh
#!/bin/bash
/opt/bin/plowdown -o /share/MD0_DATA/Qdownload/rapidshare /share/MD0_DATA/Qdownload/rapidshare/admin/down.txt 2>/share/MD0_DATA/Qdownload/rapidshare/admin/output.txt
the command that I am currently running thru ssh after I submit the form:
echo $ssh->exec('sh /share/MD0_DATA/Qdownload/rapidshare/admin/exec.sh');
Right now generates the code below but only after I kill 2 bash processes (the page keeps loading indefinetly and the processor activity is at 100% if I don't kill the 2 bash processes):
/share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 261: getopt: command not found start download (rapidshare): http://rapidshare.com/files/312885386/Free_Stuff-Your_Internet_eBay_Business_Free_Startup_Resources.rar /share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 261: getopt: command not found /share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 46: --insecure: command not found Error: failed inside rapidshare_download()
This script will be used in my local network, no access from outside, so I am not worry about security, I know the code looks very basic, primitive but I have no experience with php, shell script, so if someone can make any sense on this and help me out will be greatly appreciated.
Edit1. I also tried the shell_exec command still no joy and if I run the script thru putty works beautifully.
Edit2. I think we are on to something.
I added the code you suggested and I got the following message.
sh: /share/MD0_DATA/.qpkg/Optware/share/plowshare: is a directory /usr/bin:/bin:/usr/sbin:/sbin
I think at the moment the PATH is usr/bin:/bin:usr/sbin:/sbin and I think it should be /opt/bin /opt/sbin because there are the "executables". Any ideeas?
Thanks,
Chris.
Run this
echo $ssh->exec('pwd');
Does it list your path correctly? If so then your problem is NOT PHP, if it doesn't list or still gives an error then PHP is your problem and we can continue from there.
From the error you've listed, my first guess would be that PATH isn't set, so lib.sh can't find what it's looking for.
Remember you're logging in with a custom shell (PHP ssh), quite often things aren't set as they should be, so your scripts might not find requirements like paths and variables.
Edit:
Since it's giving /root, we at least know it's going through, why not also set the PATH etc...
echo $ssh->exec('PATH=$PATH;/share/MD0_DATA/.qpkg/Optware/share/plowshare; sh /share/MD0_DATA/Qdownload/rapidshare/admin/exec.sh');
Remember you can also use this to see what is and isn't being set.
echo $ssh->exec('ECHO $PATH');
I think I got it:
Following viper_sb logic, I changed the code to:
echo $ssh->exec('PATH=$PATH:/share/MD0_DATA/.qpkg/Optware/bin; sh /share/MD0_DATA/Qdownload/rapidshare/admin/exec.sh');
echo $ssh->exec('echo $PATH');
and magic, it worked ... I'll test it further, when I get home, but I think it worked, a file was downloaded in the /Qdownload/rapidshare folder ... hooray.