How to automatically start my application after boot?
I would like to automatically run my application after the Linux system boots. How can I achieve that?
Problem Description
The default runtime environment for PetaLinux starts many services on system boot, such as the web, telnet and FTP servers among others.
If you have developed a custom user application, using the petalinux-new-app tool, and wish to automatically start it when the system has finished booting, please use the procedure documented in this KB article.
Background
Embedded systems are typically designed to perform one task, or a small number of tasks. Further, the system is usually expected to start performing that function immediately following boot. For this reason, it is useful to be able to configure your applications to run automatically when Linux has finished booting.
This KB article assumes you have used the 'petalinux-new-app' script to create your application, and have configured the PetaLinux system to include that application in the system build. Please refer to the PetaLinux SDK "Application Development and Debug Guide" for details.
Workaround
To have your program execute at system startup, create a startup script for it inside the "user-apps/myapp" directory, and then modify the application Makefile so that this script gets installed into the root file system's "/etc/rc.d/" directory.
On startup, all scripts in this directly get run after the Linux system finishes booting.
Here is an simple startup script example:
#!/bin/sh # run myapp in the background echo "Starting myapp:" /bin/myapp &
Unless your application automatically daemonizes itself (fork()'s into the background), you will usually have to follow the 'myapp' command with the ampersand '&' character, signalling that you want the program to run in the background.
Otherwise, system startup will be blocked until your application terminates, which is not typically desired.
Assuming you have a startup script called "myappstartup" in the "myapp" directory to call your program, here is an example on how to add it to the "/etc/rc.d" directory:
- Edit the Makefile of the "myapp".
- Add the following line directly beneath the "romfs:" target of the Makefile:
[tab]$(ROMFSINST) -d -p 0755 myappstartup /etc/init.d/myappstartup [tab]$(ROMFSINST) -s /etc/init.d/myappstartup /etc/rc.d/S91myappstartup
Note that the '[tab]' sequence should be replaced by an actual tab character.
The above two lines will copy the startup script to the target root file system's /etc/init.d directory, and will create a symbolic link in the /etc/rc.d to that startup script in /etc/init.d directory.
On boot, the scripts in the "/etc/rc.d" directory are executed in alphanumerical sequence. So, you may enforce execution ordering of multiple startup scripts through their naming structure.
Our users say

