I have some cronjobs running on my linux server. These cronjobs are just executing some PHP scripts. What I want to do is to log any possible outputs these scripts would have given.
I want to use the output as given by the command:
wget -O /logs/logfile /pathofthefolder/script.php
The problem is that this command overwrites the previous logfile, thus the logfile only contains the output of the last execution, which is kinda useless for logging.
I tried adding an -a for appending instead of overwriting, but that didn't work.
I also tried with only an -a like this:
wget -a /logs/logfile http://example.com/script.php
But also that didn't work, I get the information of the download in the logfile as such:
-2014-05-27 21:41:01-- http://example.com/script.php
Resolving example.com (example.com)... [ip address of my site]
Connecting to example.com (example.com)|[ip address of my site]|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `script.php.4'
0K
So the information of the HTTP request is being stored in the logfile, and the output of every request is saved in a seperate file with increasing numbers, script.php.1, script.php.2 and so on. Which isn't quite what I want, I'd prefer to have it all in one file, I don't need the HTTP info.
Update:
So I know that it would be easier via the php or lynx command, but those commands are not installed on the server. I'm kinda stuck with the wget.
You can use this:
wget -qO- "http://example.com/script.php?parameter=value&extraparam=othervalue" >> /logs/logfile
You might want to try rewriting your cronjobs to use the php interpreter instead of wget:
php -f /path/to/file
This will execute a local (!) php file and write the output to command line.
You can easily redirect this output to apppend to a file:
php -f /path/to/file >> /logs/logfile
(to overwrite instead of append, use a single >)
If you need the error messages as well, you need to redirect both stdout and stderr:
php -f /path/to/file >> /logs/logfile 2>&1
In case you don't have/cannot install the php executable, you can use (if installed) curl to get a similar result as with wget (see other answers):
curl http://localhost/your/file.php >> /logs/logfile
Related
I have a PHP script that executes an external bash script to make an SSH connection but even though i am using ssh's move to background (-f) as well as an '&' my PHP script hangs.
Problem line in PHP script
system('/usr/local/bin/startProxy 172.16.0.5 9051');
I have also tried :
system('/usr/local/bin/startProxy 172.16.0.5 9051 &');
And the startProxy script is simply :
#!/bin/bash
#
# startProxy <IP_Address> <Proxy_Port>
#
# Starts an ssh proxy connection using -D <port> to remote system
#
ssh -o ConnectTimeout=5 -f -N -D $2 $1 &
Calling the startProxy script from command line works find and the script returns immediately.
When the system() command is run, the called script does run (I can see ssh running via ps), it just never appears to return to the PHP script.
I have run into the same issue when trying to call ssh directly via the system() method as well.
Thanks #Martin,
For future self and others, I had to change the system call to
system('/usr/local/bin/startProxy 172.16.0.5 9051 2>&1 >/dev/null');
and to change the startProxy script to :
ssh -o ConnectTimeout=5 -f -N -D $2 $1 2>&1 >/dev/null
before PHP return to the rest of the script!
Anyone able to explain why PHP stops like this if output is not redirected (I presume PHP isn't scanning the command and seeing the redirection part) and also why PHP hangs is i don't include the redirection in the ssh command within startProxy, dispite the fact that both the PHP system call and the ssh command in startProxy where using background options (-f and '&' )
I need to start a remote PHP script (example.com/cron.php) every minute with a cronjob. At the moment, my cronjob looks like this: wget example.com/cron.php. This works, but puts a cron.php file on my server every time. How can I prevent this? Or are there alternatives to wget?
Quoting from manpage for wget:
-O file
Use of -O is not intended to mean simply "use the name file instead of the one in the URL;" rather, it is analogous to shell redirection: wget -O file http://foo is intended to work like wget -O - http://foo > file; file will be truncated immediately, and all downloaded content will be written there.
That means that -O - redirects output to stdout. And output on stdout you can simply redirect to /dev/null:
wget -O - http://example.com/cron.php >/dev/null
if your sever has lynx installed you could do lynx example.com/cron.php or you could use curl and do curl example.com/cron.php
This solution is for linux server:
To execute a cron job you need to have access to cronTab on the server:
to edit crontab use the commnad line :
sudo crontab -e
add a the script you would like to execute:
* * * * * php /path/to/your/script/cron.php 2>&1
Save your crontab and you should be done.
Please check the link http://en.wikipedia.org/wiki/Cron to understand the asterisk
Check out https://www.setcronjob.com/
This web program enables you to automatically schedule cron jobs on other servers.
So my first problem is that I'm hosting on GoDaddy.
Second problem is that when I try to execute the following PHP script via SSH...
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password'
...I get a /usr/bin/php: bad interpreter: No such file or directory error. This is because on shared hosting, there is no /usr/bin/php. Instead, the php cli I want is located here:
/usr/local/php5_3/bin/php
So, I manually changed the shebang declaration in the script to...
#!/usr/local/php5_3/bin/php -q
...and sure enough, it works.
However, I don't want to have to edit this script manually. I'm looking for a way to circumvent this issue programmatically. I use this script to deploy Wordpress sites on the fly. I wget it from a git repository each time I use it. Furthermore it is deleted and installed regularly from a bash script, so modifying it in a text editor each time is not an option.
Is there a way to pass an alternate interpreter to a bash command? Like, "if you don't find the default interpreter, use this one instead".
I tried this but it doesn't work:
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password' | /usr/local/php5_3/bin/php
And because it's shared hosting I lack the permissions to symlink /usr/bin/php to the right place.
My next idea was to, via bash, edit line 1 of the php script using a sed replace command. I thought I would inquire here first for alternatives. Thanks.
If it's only this host, add /usr/local/php5_3/bin/ to your $PATH, and use this shebang:
#!/usr/bin/env php
I have a php script. I am using nginx and spawn-fcgi.
spawn-fcgi -n -s /tmp/nginx9010.socket -u www-data -g www-data -f /usr/bin/php5-cgi -C 6
How can I test from the command line that spawn-fcgi is working with the script?
e.g. I have a script in /home/ubuntu/test.php
I am having issues with nginx and executing a php script. It prompts for a download.
I have #!/usr/bin/php in the file and did a chmod a+x as well.
Thanks
For testing a FastCGI backend you could try to create a CGI environment and use cgi-fcgi to connect to the backend
You could attach with strace to see what the backend does (for example whether it even receives a request from the web server); attach with -ff to the master process to see syscalls on all workers
php5-cgi in FastCGI mode doesn't need a shebang line nor +x on the files - it doesn't use the kernel to execute them, it just loads them as simple files
Firefox (and probably other browsers too) often cache the mime type, so you will see a download prompt even after you fixed the problem. Use curl for testing!
nginx won't serve the file it passes it to php, nginx only serves static files, So if it is downloading the php file you might need to check that your are sending php files to the correct place, are you using an IP and PORT in the php location block in the config file ?
Only a guess of the top of my head whilst on the train home.
FWIW, problems like that nginx offers the file to be downloaded are due Nginx serving the files itself without sending them to fastcgi backend, often because of try_files or wrong location {} block matching to the uri.
folks,
i am very new to PHP, on project that i do now(PHP + Linux), i need to schedule tasks. I know that i can do it using cron. How can i check crontab file from PHP(i have only ftp access to server)?
I mean i can run 'crontab -l' from terminal(on my local PC). But when i try to do :
echo exec('crontab -l'); - on local PC, it retrn nothing, why it happend ? How can i run command 'crontab -l' from PHP and get output ?
I've tried to use example http://ryanfaerman.com/read/php-crontab-manager, but get compile error.
I think crontab will open the user's crontab. Your webserver might be running as root or a different user than you would be.
Try this, but I doubt it will work for security reasons:
crontab -u root -l
I got nothing from exec('crontab -l'), because i haven't permissions to run crontab and error message is in error stream, so all i need redirect error stream to out stream.
exec('crontab -l 2>&1') - that is exactly i wanted.