Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

SSH command with PHP

I have a friend who runs a media sever on his linux server and uses it to stream video.

It sometimes crashes and he then has to SSH in and restart the media server, not really a big problem but he hates having to do it.

Is there a way to do this using PHP directly?

1 Answer

You would need to set up a 'cron job' (a script that runs at specified intervals). That script could then check to see if the service is running and if it's not, restart it. However, I can't tell you how to check services that are running using php - I'm sure it's possible! Most things are :)

Robert Walker
Robert Walker
17,146 Points

I was thinking more along the lines of a button he can push in the admin panel of his site.

So he would open his admin page hit the restart button and it would run the command using php.

Ive read a few things about exec function and so on but not too clear on it all as im useless with Linux.

Ah okay, that makes sense too. If it were me, I would create it as a cron-job because then you wouldn't have to keep manually checking ;)

Try this. It's about restarting linux server, but I'm sure you can adapt to restarting different services?

Robert Walker
Robert Walker
17,146 Points

Thanks Tom I will have a good look and see if I can use it, I did want to stay away from a cron job though because I was thinking of making a start and stop button for him in the admin panel.

Its given me a lot to work with still as I know nothing about this at all so again thank you!

Ah yes, a start stop would make more sense. I guess it would be useful if the service is likely to fail without manually turning it off. I've learnt new things too! I didn't know you were able to restart the server from php! Seems kinda risky though, if it ever failed to come back up you'd be scuppered.