1

I am using Ubuntu Server 14.04 on my server. I have a PHP file that contains some script/code that I want to run when I turn my serveron.
I want to run this XXXXXX.php file on my server startup before login not after login.
I didnt have any desktop so tell me the process through terminal. My desired PHP file that I want to run is in /var/www/XXXXXX.php folder.

Now can I do it?

Edit: My XXXXXX.php contain the following codes...

<?php 
$externalIp = file_get_contents('http://phihag.de/ip/');
?> 
<form action="Iframe.php" method="post" enctype="plain" id="theForm">   
<input type="text" name="My_IP" value="<?php echo $externalIp;?>" />
<input type="submit" value="Send" />
</form>
<script type="text/javascript">
window.onload = function() {
var form = document.getElementById("theForm");
form.submit();
}
</script>
Run CMD
  • 432
  • 4
  • 14
Muhammad Hassan
  • 253
  • 1
  • 6
  • 14
  • You'll have to state whether your PHP script is supposed to run once (shortly) on boot, or perform a kind of permanent task. Also, although a web server is not required to run PHP scripts, it's quite unusual to write standalone PHP scripts. So the second question is, do you have a standalone PHP script or not. – Run CMD Jul 31 '14 at 07:54
  • @ClassStacker My question is edited and I just want to run my `XXXXXX.php` file once on startup/boot... – Muhammad Hassan Jul 31 '14 at 09:40

4 Answers4

1

You could also edit /etc/rc.local - it runs later in the start up process than cron tasks. I had to use it to fire off some MySQL stuff on re-boot because mysql wasn't up and running when the cron task fired.

EDIT:

Run this: `sudo nano /etc/rc.local'

Add your command like so ...

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#!/bin/bash

/var/www/XXXXXX.php

exit 0

good luck! :)

Simon
  • 303
  • 3
  • 4
  • 13
  • I dont have MySQL work in my `XXXXXX.php` File. I Also Shared my `XXXXXX.php` File code above. You can view my edit. – Muhammad Hassan Jul 31 '14 at 09:42
  • Yes, `/etc/rc.local` is the right mechanism. – Run CMD Jul 31 '14 at 09:45
  • @ClassStacker I am new in Ubuntu so can you explain the steps how to run my `XXXXXX.php` file through `/etc/rc.local` using terminal only...??? – Muhammad Hassan Jul 31 '14 at 09:54
  • @MuhammadHassan You should not follow your PHP idea. You would have to have a JavaScript enabled browser and a web server installation just for that. See [my answer](http://askubuntu.com/a/504935/308343) for a much leaner approach. – Run CMD Jul 31 '14 at 10:00
  • @Simon So though these codes in my `/etc/rc.local`, Will it run my HTML,JavaScript,PHP codes inside the `XXXXXX.php` file and also will sent the value through `method="post"`? – Muhammad Hassan Jul 31 '14 at 10:08
  • Now, there's the mystery! Will it work? I'm not sure as I have not tried to run a php file like yours using rc.local. I'm guessing it won't work because you do have functions in your php file that require a browser with javascript enabled just as Class Stacker points out. Perhaps it would be easier to take another approach? What is your goal? What does that php script do for you? Perhaps there is another way? I'm thinking `wget` to fire off the web page perhaps? – Simon Jul 31 '14 at 10:15
  • After converting my PHP file to Shell Script, I followed your steps having `./ShellScript.sh` and able to run my Shell Script on server boot. Thanks... – Muhammad Hassan Aug 01 '14 at 15:15
1

You may have thought that the solution is just around the corner, but I think you need to go one step back.

What you need to do appears to be a kind of automatic registration. Smart idea, yet... your current approach will require a browser supporting JavaScript.

I think I would do that differently. At the end of the day, all you need to do is issue the POST command which the browser sends.

That's much easier than all the PHP and JavaScript.

Run CMD
  • 432
  • 4
  • 14
  • I got your idea that I have to make a script in which I have to get my IP then send it through `curl --data "myIP=XX.XX.XX.XX" http://www.demo.com/Iframe.php` and then add that script file in `/etc/rc.local`. Did I get you right? – Muhammad Hassan Jul 31 '14 at 10:06
  • @MuhammadHassan Absolutely. :) – Run CMD Jul 31 '14 at 10:34
0

You can do it with php5-cli. If you haven't installed it already:

sudo apt-get install php5-cli

Then you can run php commands from commandline with: php5 ./script.php.

As first line of your php script add the following:

#!/usr/bin/php5

Then create a link to your script and enable it to run on start with:

cd /etc/init.d
sudo ln -s /path/to/your/scriptName.php scriptName
sudo update-rc.d scriptName defaults
sudo update-rc.d scriptName enable
Pabi
  • 7,351
  • 3
  • 40
  • 49
0

If the script should perform a one-shot task at startup I would use a @reboot cron task. In this case, you should edit your crontab with sudo crontab -e and add something like the following line:

@reboot php /var/www/XXXXXX.php

This page provides additional details on using cron.

  • **Attention :** the tasks using *@reboot* will run as soon as the cron daemon is started, which can happen before other daemons that may be required by your script (like database or SMTP) are up and running. – Benoit Jul 31 '14 at 08:58
  • I'm with @Benoit; this is not a good advice. – Run CMD Jul 31 '14 at 09:43
  • I have to hit a webpage in my `XXXXXX.php` file also to get some value from there then have to run the rest codes on that php file. My question is edited with `XXXXXX.php` code. – Muhammad Hassan Jul 31 '14 at 09:44
  • I absolutely agree that _@reboot_ cron tasks are executed quite early (so not everything can be executed like this). Still, if the script is dependent on other services I would use upstart jobs rather than the `rc.local` solution. – user1366204 Jul 31 '14 at 17:09