Forums » Software Development »
How to launch application on startup
Added by Mary Frantz about 11 years ago
Could you provide an example of how to start an application on startup. I get that you can run it from a script in init.d which is referenced in rc5.d. My problem is that if I start it in the background (even from a shell command) it still seems to run only for a little while, then nothing. I think, anyway. How can you tell? I see printf output for a few seconds, then nothing. The application is mult-threaded and includes two servers that a host PC interact with. My host PC will not connect to either server that is supposed to be running.
Can it be launched in such a way that printf still outputs to the console? I have no GUI, so it's hard to tell what's going on without some kind of output.
Here's my script (located in init.d, with a link in rc5.d which runs last):
#!/bin/sh echo "Changing cpu clock to 456MHz" echo 456000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed # See settings in /etc/network/interfaces #echo "Resetting ip address" #ifdown eth0 #ifup eth0 # to use a file besides /etc/network/interfaces #ifup eth0 -i somefilename # 2 pools: pool0 = 2Mb at 0xC3E00000, pool1 2Mb at 0xC3C00000, pool1 = 32Mb at 0xC4000000 echo "Inserting cmem module" insmod /lib/modules/3.2.0/extra/cmemk.ko pools=1x33554432,1x2097152,1x2097152 phys_start=0xC3C00000 phys_end=0xC5FFFFFF echo "Inserting dsplink module" insmod /lib/modules/3.2.0/extra/dsplinkk.ko echo "Starting my application" ./home/root/HelloWorldDSP/Debug/myApplication myArgument & exit 0
thanks,
Mary
Replies (6)
RE: How to launch application on startup - Added by Jonathan Cormier about 11 years ago
Mary,
I think you have a typo here:
./home/root/HelloWorldDSP/Debug/myApplication myArgument &
There shouldn't be a period at the beginning of the line.
RE: How to launch application on startup - Added by Jonathan Cormier about 11 years ago
You can run the command "ps | grep myApplication" and look for your process in order to tell if its running.
Also normally you would redirect output from the console to a log file for your application, something like:
/home/root/HelloWorldDSP/Debug/myApplication myArgument > /var/log/myApplication.log 2>&1 &
RE: How to launch application on startup - Added by Jonathan Cormier about 11 years ago
Do you happen to know if your system is using systemd for starting processes?
You can check to see if the following directory exists: /etc/systemd/
If you have a filesystem that uses systemd then reading the following may be helpful.
http://support.criticallink.com/redmine/projects/arm9-platforms/wiki/Linux_Startup_Process_-_Systemd
RE: How to launch application on startup - Added by Bob Duke about 11 years ago
Mary,
When you exit a shell, all processes connected to the shell are typically terminated. When you execute a script, the shell used to run the script terminates at the end. Your application is probably being terminated prematurely.
In order to have a program run without a terminal, you need to write it as a daemon or use the nohup command:
nohup /path/to/program
This will launch the program and detach it from the terminal, so it will continue to run after the script is finished.
By default, the nohup will place any output in a file called nohup.out which you can monitor for output (or you can redirect the output as Jon suggested).
You may have issues running your application with nohup, but try that as a first step.
I suggest removing the script from the startup process until you can get it to run from the command line.
-Bob
RE: How to launch application on startup - Added by Mary Frantz about 11 years ago
So, from the command line:
nohup /home/root/HelloWorldDSP/Debug/myApplication myArgument &
This works. My PC host can connect and communicate and all the printf output ends up in nohup.out.
And I can redirect the output so that it doesn't forever fill up a file.
nohup /home/root/HelloWorldDSP/Debug/myApplication myArgument > /dev/null 2>&1 &
So, I can add one of the above lines to my script when I am ready to run at startup.
Is there any benefit to using systemd and writing a .service file?
Does nohup manage the size of nohup.out so it doesn't grow indefinitely? I expect the application to run forever in the end product. A log file would be nice for trouble shooting, but would have to have limits placed on it.
Is there any way to send output to the serial port from the background? I tried redirecting the output to /dev/ttyS1 but got continuous errors "too much work for irg53" and had to power cycle.
Mary
RE: How to launch application on startup - Added by Bob Duke about 11 years ago
Is there any benefit to using systemd and writing a .service file?
Mary, systemd does provide many more features (like process monitoring and restart capability) that may be useful depending on your application. Using systemd effectively, however, does require some time to understand how it works.
Does nohup manage the size of nohup.out so it doesn't grow indefinitely? I expect the application to run forever in the end product. A log file would be nice for trouble shooting, but would have to have limits placed on it.
Nohup does not manage the file size. There are multiple approaches you could use depending on what you need. It may be easiest to have your application write specific troubleshooting messages to a particular file (bypassing nohup.out). Alternatively you could pass the nohup output to split and have it create multiple log files. Then, you would use a cron job to delete older ones.
Is there any way to send output to the serial port from the background? I tried redirecting the output to /dev/ttyS1 but got continuous errors "too much work for irg53" and had to power cycle.
You may need to use stty to configure the serial port (e.g. stty -F /dev/ttyS1 9600 -parity cs8, don't use these settings this is just an example) before sending data. The program collecting data from the serial port will need to have the same settings.