How can I run a php script on a remote server? - php

I have a remote Apache/2.2.16 (Debian) Server where I want to do intervalled jobs like every 2 hours do this, at 12.00 do this. So, I wrote a php file which can do this but I have to start it via the browser and have to keep the browser window open to keep the script running. I heard I could execute a php script via console, but I don't have access to it.
How can I run the script without access to the console and without having my browser opened?

Set up a cron job for this task. You can set this up on the server or on any other computer that runs at the given time.
On a Linux or Mac OS machine, open a terminal and type crontab -e.
Add something like this to the end of the file:
0 12 * * * wget http://url.com/your/script.php -O /dev/null
This will call the script every day at 12:00. Make sure wget is installed.

Related

Run php script from command line endless

I have a file named /root/folder/myfile.php that will handle incoming packets from a specific port by a GPS device.
When I use [root#main ~] php /root/folder/myfile.php, everything works fine.
I need this file run every second to listen.
I researched for a while and figured out that using php cli is a solution, so I tried above command but as long as the shell is open (I'm using PUTTY), file is executing and when I close the shell, process will be killed.
How can I (where can I) add a command that will run this file every second, or may be in realtime?
I'm using linux centOS 6.5.
Thanks in advance
nohup php myscript.php &
the & puts your process in the background.
The solution from Run php script as daemon process
To kill it:
1) display all running proceses with: ps aux | less or top command
2) find pid(process id) and kill with: kill pid
You would want to use the cron functionality of your server.
Similar to this maybe:
running a script from cron every second

Hitting URL every minute in background

I want to hit a URL of my application through cron job. I have done the following things:
1) Opened the terminal
2) Did crontab -e which gives me an editor that allows me to put statements
3) Pasted the URL that needs to be hit after specified interval of time:
curl -s http://www.example.com/controller/function_to_execute
This cron job will run every minute
4) Saved the cron and again on terminal did crontab -l, and I could see my cron
This scheduler isn't working, don't know why. I tried curl on the terminal directly expecting some output, but after 5 minutes it gives me the result
curl: (7) couldn't connect to host
If I hit my URL on browser directly then my job gets successfully executed!
Have I made some mistake while making entry in the cron?
First you need to create the php script and run the script using cronjob
1)Open terminal and type crontab -e
2)Edit the file and write the following code to run the php script in background
*/1 * * * * php /yourpath/yourphpfile.php
3) Create yourphpfile.php and write the code to hit the url
you have to execute and test the yourphpfile.php before doing cronjob
Whoops!..tried to just curl www.google.com and gave the following output in form of HTML tags "302 Document has moved", Also I am not able to ping the application itself from the terminal but it is accessible publicly
So cron job getting executed is out of question :P

Run a php script on localhost using cron job

I am trying to set up a cron job on my localhost to call a php script every half an hour 24/7. Following are my steps:
1) I have created a cron.php file to call myscript.php:
<?php
echo exec('*/30 * * * * C:\BitNami\wampstack-5.4.21-0\php -f C:\BitNami\wampstack-5.4.21-0\apache2\htdocs\myscript.php');
?>
2) Created a cron.bat file to run the cron.php on browser:
"C:\Program Files\Mozilla Firefox\firefox.exe" "localhost\cron.php"
3) Then I have set up a windows 7 task scheduler to call cron.bat every day.
I welcome any idea on how to set up a cron job as my approach doesn't do anything.
Thanks
This is not an answer, but only pointers you need to check before you proceed further.
Crons are available only on unix systems, not windows.
While debugging cron or anything similar, schedule them for every minute or (any such acceptable smaller frequency), and once they work every minute, only then schedule them for the original frequencies. That will save you some time during debugging.
Its an incorrect call to exec. You don't have to pass all those * in it
Since this is windows, You want to do php.exe and not C:\BitNami\wampstack-5.4.21-0\php -f which would have been done in linux (with proper path)
Your .bat file is currently scheduled daily, you need to schedule it every half hour.
you don't need to creat .bat file to run php script
follow the step to setup cron job for php interpreter..
step 1- Then I have set up a windows 7 task scheduler to call php script by giving path of php interpreter like C:\xampp\php\php.exe filename.php by setting desired interval
dude, you cannot define cron jobs in wamp,
the cron is a linux feature and you are running the wamp on windows.
you can run the script from the command line (of from cygwin, much better),
something like "/php.exe "
but you cannot define crons on wamp
but you can setup the scheduler to run a .bat file that contains the next code
cd/
cd "Program Files (x86)"
cd GnuWin32
cd bin
wget.exe http:// localhost/... (your file)
echo DONE

How to run php jobs regularly in my Amazon EC2 server?

I have a Amazon EC2 server.
And I would like to run php jobs every 1 hours to get the latest information from websites and insert them into database.
But I am not good at server.
Any suggestions?
From this URL, you call crontab from the shell and to execute every hour, add the "00" at the beginning, point to the location of PHP and the script you want to run.
# crontab -e
00 * * * * /usr/local/bin/php /home/dir/myscript.php
As for any Linux server, use crontab.
On the terminal (SSH) :
crontab -e
See the man page for cron

How to run a php script through the command line (and keep it running after logging out)

I am trying to run a php script on my remote Virtual Private Server through the command line. The process I follow is:
Log into the server using PuTTY
On the command line prompt, type> php myScript.php
The script runs just fine. BUT THE PROBLEM is that the script stops running as soon as I close the PuTTY console window.
I need the script to keep on running endlessly. How can I do that? I am running Debian on the server.
Thanks in advance.
I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.
You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.
nohup php myscript.php >myscript.output 2>&1 &
This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.
The final & causes the script to run in the background so you can do other things whilst it is running or logout.
An easy way is to run it though nohup:
nohup php myScript.php &
If you run the php command in a screen, detach the screen, then it won't terminate when you close your console.
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.

Categories