Jump to content

command line php script to run at startup? Suggestions?


crees

Recommended Posts

Hi all

I have a PHP script that runs at the root command line on linux (uses ttyUSB0) and basically monitors the port to send and receive data(does not get executed via web). 

I would like to have this script startup during the boot process and then also be cleanly shutdown on restart or shutdown. Do any of you have a suggestions on how to do this? 

Right now I simply execute the script by the PHP alcd.php at the command line in the directory of the alcd.php file.  when I want the script to stop I hit control C since it runs a while (true) statement. However the code is written to time out every 5 seconds and check to make sure there are no other commands to proccess so I could have it check an xml, mysql, text data entry to see if it should stop, restart etc.

Thanks in advance
Link to comment
Share on other sites

It is possible to have a script run at start-up or shutdown.  In this case it may be worth making this a seprate script that prompts your main script.  How you do this depends very much on what distro you are running.  Havent done this for a while but i seem to remember placing the script in /etc/init.d and then running somthing like "rc-update add <script> default".  Idealy you want to define it as a service. 

By far the simplest way to do it however would be to add it as a cron task.  If cron's not an option, let us know your linux distro and perhaps we can tell you more.
Link to comment
Share on other sites

thanks for the reply

I am using centos

I prefer to run the php CLI file as a service if I can.

I have also looked at putting it as a cron job.  If I do run it in the cron system how would i tell it to execute once on start-up? I think it would be easier this method but wouldn't mind learning both ways

a little more background,   I have succesfully got a php based script to manage the Crystalfontz 635 USB LCD and send program stats, uptime etc.  I am currently designing menu systems, but before I get to carried away I want to make sure I can get the base PHP script to run after bootup and listen for keypresses and other info via xml, mysql etc.  I chose PHP because its easier for me to understand (althoe im not the best with it)  and it seams that php provides the CLI to allow stuff like this to run.

Thanks again.



Link to comment
Share on other sites

Sounds intresting!  I also use php for similar resions!

Anyway..... The easyest way would be to add it to /etc/rc.local .  It can also be added to /etc/inittab.  The main diffrence hear is that anything in rc.local is run after everything in init.  If your script is reliant on other services this feature of the rc.local file may be usfull.

The other option is to make it a standard init script.  This will then run as a service, and you can controle its run level like any other.  If you wish to go down this way it would be best to read up on chkconfig and things!  i dont really know/remember enough to help you hear.  In some situations this is prefrable for odvious resions, but some of the experts say it also makes the script more visible. (that a little above me!).  I asume its if your going to be calling them from else where.

Hope some of this is helpfull.
Link to comment
Share on other sites

[quote]If I do run it in the cron system how would i tell it to execute once on start-up?[/quote]

That is not at all what cron is for. Cron is for running a job at a certain time. You'll want to look at the CentOS docs for init scripts, most distros handle these a little differently from each other.
Link to comment
Share on other sites

[quote author=thorpe link=topic=121113.msg498679#msg498679 date=1168140729]

That is not at all what cron is for. Cron is for running a job at a certain time. You'll want to look at the CentOS docs for init scripts, most distros handle these a little differently from each other.
[/quote]

Tanks for the clarification.  I will check the init scripting in Centos to see if I can get a clearer understanding.  Perhaps it will be easier to do as neylitalo stated and have it launched at startup.  Only problem that is I would like it to run withoug tying up roots console login. (ie when I lauch the script its runs great but I cannot run anything else on the colole untill I kill it).  Whats the best method of running it in the background with root access? Or is this getting back to biulding it as a service and running it in the init stuff?

Thanks again

crees
Link to comment
Share on other sites

Thanks again for the help.  executing the script with in the backgound caused it to run untill an echo or exec statement was issued in the php code. 

I found another method yesterday and use the open -l PHP alcd.php command.  I had to yum the open program to get it on my centos box.  now the program with the -l loads the program to be executed in another ttl terminal with the same permisstions as the logged in user (im my case root) 

Now it runs great and is out of the way of tying up the console. 

Thanks again for your time.
Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.