# Restart FreeBSD via php code



## birender (Dec 21, 2017)

1.I have a FreeBSD Server at a remote location and I want to restart the server via php code.
2.And I have a python file which I want to start that application via php code after rebooting my server.
I need advice on this


----------



## poorandunlucky (Dec 21, 2017)

```
<?php
exec reboot;
?>
```


```
<?php
exec script.py;
?>
```


----------



## birender (Dec 22, 2017)

poorandunlucky said:


> ```
> <?php
> exec reboot;
> ?>
> ...



Doesn't it require root password?


----------



## MarcoB (Dec 22, 2017)

Not if user is in the operator group.


----------



## ralphbsz (Dec 23, 2017)

You want one application to reboot the machine.  That's pretty easy, see above, as long as the application is running as root or at least in the operator group.  If your application is not running as root, one option is to use sudo: let your script execute sudo with suitable arguments, and configure sudo.

The harder part is your second request: you want to start a PHP script upon booting. That's not easy, because after booting, all "state" or "memory" of what was running before boot has been forgotten.  I would propose the following: make your application into a service (see man rc, or read the handbook), and start the service upon booting.  That requires you to learn how to write rc.d scripts, but that's not very difficult, and a good skill to have.

Where it gets really gnarly: You might want to not *always* start that application on every boot, only if your application requested that it be started on the next reboot.  There are many ways to accomplish it.  Here's what I would do: Before rebooting, have your application leave some information in a well-known place, like a status file in the file system.  When the new service starts, have it look at that well-known place, and if it says there that it doesn't need to run, immediately exit.


----------



## poorandunlucky (Dec 23, 2017)

```
#include <stdlib.h>

int main {
system("script.py");
return 0;
}
```

cc file.c

Add a cron job.


----------



## ralphbsz (Dec 24, 2017)

That C program is actually not even necessary; you can add script.py directly to cron, if the first line has a she-bang in it.  Most of my quick and dirty hacks are written in scripting languages or interpreted languages like python, and quite a few of them end up being cron jobs or rc.d daemons.


----------



## birender (Jan 5, 2018)

wow there are lot of replies.I didn't expect this.

Happy new year to everyone on this forum

My problem is I have webpages on  hostgator and FreeBSD server is on different host.
I have a file called sta.py in path /home/im2.
I want to start this file after rebooting.
But before that I want to connect my FreeBSD using php script via ip and root user and pass to reboot it

So I don't know how to accomplish this.
I need a serious help.
A sample php code will help me


----------



## poorandunlucky (Jan 5, 2018)

What?

I think I read:

- I want to run /home/im2 after booting; and
- I want to reboot through a PHP script.

I already explained how to reboot using PHP; as for running a script on boot: that's accomplished through RC scripts, like those found in /etc/rc.d.  You can copy one of those scripts, edit it to fit your purpose, and add a line in rc.conf to run that script on startup.

I think there's a very bold line between "explaining how to do something" and "doing something for someone else", and I'm afraid we've reached that point.  You're going to have to write your scripts yourself, and dedicate all of 20 minutes to figure out how rc scripts work.

Good luck!


----------



## birender (Jan 5, 2018)

poorandunlucky said:


> What?
> 
> I think I read:
> 
> ...




Well I am not good in PHP as I am good in FreeBSD but any ways thanks for taking interest in clearing my doubts.
I will figure out myself and will update the status here.


----------



## birender (Jan 24, 2018)

I used phpseclib and it worked like a charm.
Thanks to poorandunlucky for his is small script and other forum members for helping me.
I used with some modification and all went OK


```
<?php
exec reboot;
?>

<?php
exec script.py;
?>
```


----------

