crees Posted January 5, 2007 Share Posted January 5, 2007 Hi allI 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 Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/ Share on other sites More sharing options...
the_oliver Posted January 6, 2007 Share Posted January 6, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-153911 Share on other sites More sharing options...
crees Posted January 6, 2007 Author Share Posted January 6, 2007 thanks for the replyI 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 waysa 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. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-153929 Share on other sites More sharing options...
the_oliver Posted January 6, 2007 Share Posted January 6, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-154406 Share on other sites More sharing options...
trq Posted January 7, 2007 Share Posted January 7, 2007 [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. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-154732 Share on other sites More sharing options...
neylitalo Posted January 7, 2007 Share Posted January 7, 2007 In Gentoo Linux, you can put commands to execute on start-up in /etc/conf.d/local.start. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-155106 Share on other sites More sharing options...
crees Posted January 8, 2007 Author Share Posted January 8, 2007 [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 Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-156133 Share on other sites More sharing options...
neylitalo Posted January 8, 2007 Share Posted January 8, 2007 To start a program in the background:[code]$ yourprogramhere &[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-156202 Share on other sites More sharing options...
crees Posted January 9, 2007 Author Share Posted January 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-156693 Share on other sites More sharing options...
c4onastick Posted January 29, 2007 Share Posted January 29, 2007 [quote author=neylitalo link=topic=121113.msg500164#msg500164 date=1168299541]To start a program in the background:[code]$ yourprogramhere &[/code][/quote]To get rid of the echo and exec problems you might try dumping the stdout:[code]$ yourprogramhere > /dev/null 2>&1 &[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32964-command-line-php-script-to-run-at-startup-suggestions/#findComment-172126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.