Runtime Automatic Startup
About
This page shows how to set up applications to run automatically upon machine startup. As the auto-start sequence is different for each Operating System, there is one section for Linux and one section for Windows.
Automatic Run on Linux
Note: If the appliation has real-time requirements, see Linux Kernel Configuration for more information. In addition, the user account must have realtime privileges set up, as described in Linux Realtime Configuration.
CDP Studio comes with functionality to manage CDP application start, stop, autostart and decommissioning on Linux, called 'App Manager'.
App Manager
Note: This feature requires the device to be paired with a user that has root access (or a user that can read & write the /etc/ folder on the device using 'sudo').
The App Manager can be invoked by right-clicking a Linux device in the CDP Studio Deploy Configuration Devices tab and selecting 'Run Script' -> 'Run App Manager'.
Selecting that will present an interface to manage CDP Application(s) on the device. The interface can be resized to a larger size if required.
Note: Some of the operations require elevated privileges; it might be required to type the password to relaunch the script with elevated permissions.
The following information is presented:
- Header: App Manager version
- Header: Which init-system is detected, either systemd or init
- Info / Body
- Footer: The current selection
- Footer: Instructions on how to do navigation and selection.
When App Manager starts up, it will search for applications in the paired user home directory, and present them in the body section of the App Manager. Selecting an application will present the operations that can be performed on that application.
The Select operation page shows the current Running state of the application, along with the current Autostarting state of the application. The operations to be performed are listed in the body section of the App Manager.
The following operations are available:
Operation | Description |
---|---|
Set Real-time Permissions | Uses setcap (if available) to set permissions on the executable. Alternatively, /etc/security/limits.conf is set up to allow the user to run real-time processes. Note that if setcap is used, you must set these permissions each time the executable is changed. |
Start application | If auto-start is enabled, and the application is not running, App Manager will use the init-system to try to start the application. If auto-start is disabled, App Manager will start the application detached. If the application is detected as already running, nothing will happen. |
Stop application | Selecting this prompts a warning and stops the application. |
Enable Autostart | This will generate the required files and options for autostarting the application and set the autostart to enabled. |
Disable Autostart | This will remove the the files required for autostarting the application and remove the autostarting. |
Decommission application | This will prompt you to remove the application and will stop the application (if running), disable autostart, delete the application directory with all its contents and prompt to restore the limits.conf if that was changed by the App Manager at some point. |
Note: The App Manager will automatically time out after 15 minutes of inactivity.
Manual Setup of Automatic Run
For cases where the AppManager is not suitable, we have listed some examples of how to manually do automatic startup handling on Linux below. To set up automatic run on Microsoft Windows, see Automatic Run on Windows below.
The examples refer to some variables that you should replace. The below table describes the variable and what it should be replaced with.
Replace | With |
---|---|
$username | Login name of user account where the system is installed |
$network_interface | The operating system network interface name that the application will use for communication, for instance 'enp109s0f1' or 'eth0' |
$app_name | The application name, for instance 'GUIApp' |
$app_dir | The application directory, for instace /home/<username>/<system name>/<app name>/ |
Determine init-system
To determine which init-system is being used, run the following command on the target, and note the output:
ps -o comm 1 | tail -1
If the output is 'systemd', see the next section called Using 'systemd'. If the output is 'init', see the section called Using 'sysvinit'. For anything else, see the last section called Using 'crontab'
Using 'systemd'
For a graphical application, put the following content into a file in '/etc/systemd/system/CDP.$app_name.service' (replace $app_name etc as explained in the table above)
[Unit] Description=$app_name CDP Application Requires=systemd-networkd.socket After=systemd-networkd.socket [Service] Environment='DISPLAY=:0' Environment='XAUTHORITY=/home/$username/.Xauthority' Restart=always ExecStartPre=/lib/systemd/systemd-networkd-wait-online --interface=$network_interface --timeout=60 # Comment in below line to add extra sleep before starting application #ExecStartPre=/bin/sleep 0 ExecStart=$app_dir/$app_name WorkingDirectory=$app_dir Type=simple User=$username TimeoutStartSec=infinity [Install] WantedBy= graphical.target
For a non-graphical application, remove the two 'Environment' lines above, and change 'graphical.target' to 'default.target'.
To enable the service (automatic startup), run:
sudo chmod +x /etc/systemd/system/CDP.$app_name.service sudo systemctl enable CDP.$app_name.service sudo systemctl daemon-reload
To disable the service, run:
sudo systemctl disable CDP.$app_name.service sudo systemctl daemon-reload
To start the application run:
sudo systemctl start CDP.$app_name.service
To stop the application run:
sudo systemctl stop CDP.$app_name.service
Using 'sysvinit'
For applications using the sysvinit, put the following content into /etc/init.d/CDP_$app_name: (replace $app_name etc as explained in the table above)
#!/bin/sh PREFIX="StartCDP: " CDPDIR="$app_dir" CDPAPP="$CDPDIR/$app_name" do_start() { my_ip=$(ip -f inet addr show $network_interface | sed -En -e "s/^\s*inet[[:blank:]]([0-9.]+).*/\1/p") timeout=60 echo "${PREFIX}Waiting for network (max 60 seconds)..." while ! ping $my_ip -c 1; do sleep 1; timeout=$((timeout-1)) if [ $timeout -eq 0]; then break; fi done # Comment in the below lines to sleep before starting the application # echo '${PREFIX}Waiting 0 additional seconds before starting app' # sleep 0 echo "${PREFIX}starting" cd $CDPDIR if start-stop-daemon --start -q -o -b -p /var/run/$app_name.pid -m -c $username --exec "${CDPAPP}"; then echo "${PREFIX}done" else echo "${PREFIX}error, could not start CDP Application ${CDPAPP}!" fi } do_stop() { echo "${PREFIX}stopping" if start-stop-daemon --stop -q -o -p /var/run/$app_name.pid --exec "${CDPAPP}"; then echo "${PREFIX}done" else echo "${PREFIX}error, could not stop CDP Application ${CDPAPP}!" fi } case $1 in start) do_start ;; stop) do_stop ;; restart) do_stop do_start ;; *) echo "${PREFIX}usage: ${0} [start|stop|restart]" ;; esac
Make the script runnable:
sudo chmod +x /etc/init.d/CDP_$app_name
To make it start automatically, run:
sudo ln -s /etc/init.d/CDP_$app_name /etc/rc.d/S99_CDP_$app_name
To remove it from automatic start, run:
sudo rm /etc/rc.d/S99_CDP_$app_name
To start the application manually run:
sudo /etc/rc.d/S99_CDP_$app_name start
To stop the application run:
sudo /etc/rc.d/S99_CDP_$app_name stop
To remove the script, run:
sudo rm /etc/init.d/CDP_$app_name
Using 'crontab'
Assuming we have an application deployed to Raspberry Pi (192.168.1.10), deployed to
/home/pi/TestSystem/TestSystemApp
In the following, type in the password for user 'pi' whenever prompted, and press the Enter key.
- Log into the Raspberry Pi using ssh from bash, or putty.exe: (default username/password for Raspberry Pi is: pi/raspberry)
ssh pi@192.168.1.10
- Using your favorite text editor (f.e. 'nano') create startup script /home/pi/TestSystem/TestSystemApp/start.sh with contents:
#!/bin/sh AppName=TestSystemApp echo "Starting $AppName" # Wait for a network connection while ! ping 8.8.8.8 -c 1; do sleep 0.5; done # while ! ping 192.168.0.1 -c 1; do sleep 0.5; done # For systems not connected to the public internet ./$AppName &
- Then make the script executable, performing:
sudo chmod +x /home/pi/TestSystem/TestSystemApp/start.sh
- Now, to make the script auto-start during machine startup, add corresponding crontab @reboot line. Invoke crontab in edit mode, typing
crontab -e
and add following line at the end of crontab file:
@reboot (cd TestSystem/TestSystemApp; ./start.sh)
Resilience Against Ungraceful Shutdown
Configuring automatic startup does not mean it is safe to simply unplug a device without first closing all CDP applications. This can result in empty or corrupt files if they were being modified at the moment of power loss. For increased safety, one can enable data journaling on the file system level in Linux. This will reduce file system performance, but also the risk of data corruption.
For example, on a Raspberry Pi with default configuration (using ext4 file system and keeping data on '/dev/mmcblk0p2' partition), run the command:
sudo tune2fs -O has_journal -o journal_data /dev/mmcblk0p2
After that reboot your device.
Automatic Run on Windows
A typical way to autostart an application on windows is to put a shortcut to it in the Windows 'Startup' folder. Below is an example of how to do this.
Assuming we have an application deployed to Windows, located in
C:\CDPStudioWorkspace\deployments\systems\TestSystem\TestSystemApp
- Log into the Windows computer.
- Open Windows explorer (Windows-key + E) in the folder where the TestSystemApp is deployed.
- In Windows Explorer, right-click the application, select 'Create Shortcut'.
- Right-click the shortcut and select "Copy"
- Press Windows-key + R and type "shell:startup", press Enter
- In the folder that opens, right-click and select "Paste"
The application will now start up each time that user logs in to Windows.
Note: Windows is not a real-time environment. This means that the application may be unresponsive for several hundred milliseconds - to seconds, due to the operating system prioritizing other tasks. If the application has real-time requirements, consider runnning it on a real-time capable Linux.
To remove the application from automatic run:
- Press Windows-key + R and type "shell:startup", press Enter
- In the folder that opens, delete the link to TestSystemApp
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.