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 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
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
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,429
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.