And making it run on start-up
September 21, 20233 minutes

When designing custom PetaLinux builds, we often end up with a bunch of commands that we have to run after login to set things up and make initializations. Having a script built into the root filesystem can make this process easier, so in this post I’m going to show you how you can set that up in PetaLinux 2022.1.
These instructions assume that you already have a PetaLinux project created.
We don’t actually want to create an application, it’s just the best way that PetaLinux allows us to do this.
Source the PetaLinux settings script:
source <path-to-petalinux-install>/2022.1/settings.shCd to your PetaLinux project and create a new application called myscript:
cd my_project
petalinux-create -t apps --name myscript --enableLet’s go through what exactly that command has done:
To file my_project/project-spec/configs/rootfs_config, it has added:
CONFIG_myscript=yTo file my_project/project-spec/meta-user/conf/user-rootfsconfig, it has added:
CONFIG_myscriptTo folder my_project/project-spec/meta-user/recipes-apps, it has added these files and folders:
+-- myscript
+-- files
| +-- Makefile
| +-- myscript.c
|
+-- myscript.bb
+-- README
+-- .gdbinit These files make up a template C application, but we don't want this, we just want to add a bash script.
So let's continue with the steps.
## Step 3: Modify the template
1. Delete the following files; we don't need them:
* `my_project/project-spec/meta-user/recipes-apps/myscript/files/Makefile`
* `my_project/project-spec/meta-user/recipes-apps/myscript/files/myscript.c`
2. In that same `files` folder, create the following script and save it as `myscript`:
```bash
#!/bin/bash
echo "The Matrix has you..."myscript.bb file and replace it’s contents with the following:#
# This file is the myscript recipe.
#
SUMMARY = "My script"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://myscript \
"
S = "${WORKDIR}"
RDEPENDS:${PN} += "bash"
do_install() {
install -d ${D}${bindir}
install -m 0755 ${WORKDIR}/init_cams ${D}${bindir}/
}myscript script to the /usr/bin folder so that we will be able to
execute it from anywhere.Rebuild the PetaLinux project:
petalinux-buildOnce the PetaLinux project has built, we will be able to boot it on our hardware and login.
From the command line, we should be able to run our script by typing myscript:
myproject:~$ myscript
The Matrix has you...Now what if we want this script to run automatically on start-up. The following changes need to made:
Create a file called myscript.service with the following content and place it in
my_project/project-spec/meta-user/recipes-apps/myscript/files:
[Unit]
Description=myscript
[Service]
ExecStart=/usr/bin/myscript
StandardOutput=journal+console
[Install]
WantedBy=multi-user.targetReplace the contents of myscript.bb with the following:
#
# This file is the myscript recipe.
#
SUMMARY = "My script that runs on boot"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://myscript \
file://myscript.service \
"
S = "${WORKDIR}"
FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
inherit update-rc.d systemd
RDEPENDS:${PN} += "bash"
INITSCRIPT_NAME = "myscript"
INITSCRIPT_PARAMS = "start 99 S ."
SYSTEMD_PACKAGES = "${PN}"
SYSTEMD_SERVICE:${PN} = "myscript.service"
SYSTEMD_AUTO_ENABLE:${PN} = "enable"
do_install() {
if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then
install -d ${D}${sysconfdir}/init.d/
install -m 0755 ${WORKDIR}/myscript ${D}${sysconfdir}/init.d/
fi
install -d ${D}${bindir}
install -m 0755 ${WORKDIR}/myscript ${D}${bindir}/
install -d ${D}${systemd_system_unitdir}
install -m 0644 ${WORKDIR}/myscript.service ${D}${systemd_system_unitdir}
}
FILES:${PN} += "${@bb.utils.contains('DISTRO_FEATURES','sysvinit','${sysconfdir}/*', '', d)}"This new recipe not only copies the script to /usr/bin, it also sets it up as a service
to run on boot.
Rebuild the PetaLinux project:
petalinux-buildNow you should be able to boot the new PetaLinux build and see in the boot log that your script has run.