LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automate commands just after boot in a Busybox environment (https://www.linuxquestions.org/questions/linux-newbie-8/automate-commands-just-after-boot-in-a-busybox-environment-4175525718/)

django84 11-17-2014 08:19 PM

Automate commands just after boot in a Busybox environment
 
Hello everyone,
My name is Rick, and I'm glad to have found a nice forum like this ;D
I have a camera which loads Busybox. The camera has a Compact Flash card installed in it, which has one partition in it, formatted in the ext2 file system.

I would like the camera to automatically run these commands, just after boot has finished:
1. do a check on the /dev/hda1 file system, since sometimes when the partition is not unmounted by terminal (like after a power cycle or reset), Busybox warns about the FS flag.
"e2fsck -p -c /dev/hda1"

2. mount the hda1 partion:
"mount /dev/hda1 /mnt/0/

How can I automate these two commands to be executed as the last two of the boot process?
Thanks a lot!

michaelk 11-19-2014 08:02 AM

Without knowing anything about the camera's operating system it is difficult to say. My first guess would be to look in /etc/ and see if there is are any rcx.d directories. What ever directory except for rc0.d and rc6.d that has some files would be the one to add a script of your commands.

Files in that directory start with a SXX where xx is a number from 0 - 99. S99 would be the last process to start. I assume that the recording process would be the last to start and you really want to run your script before it that. You might be able to determine what process actually mounts /dev/hda1.

django84 12-04-2014 01:02 PM

Thank you michaelk, sorry for my late response but I had to pause working on this project for a brief amount of time.
In the /etc dir, there are folders from rc0.d to rc6.d, and inside these folders there are several scripts that start with K or S; then there's a folder called rcS.d.

Inside it, there are these scripts: S05issue, S15var, S23urandom, S27resolv, S10mountall, S20mac, S25hostname, S94net.conf.
Now, Do I have to add a new script, something named S95user to this folder, or do I have to add my own code to the s94net.conf file?

What this script has to do is just to check the CF, mount it, then copy a file from the /bin folder to a folder in the CF where the snapshot are taken, and add this folder on the CF to the $PATH.
The snapshot will happen when a single board computer, connected to the camera through RS232, will execute the script copied during the boot.

Let me know if you need any additional info on the camera/system. Thanks again!

michaelk 12-04-2014 07:30 PM

You would create a new script S95xxx.

The output of the command runlevel should indicate the current runlevel which would indicate what rcx.d to add the script.

suicidaleggroll 12-04-2014 07:33 PM

Alternatively, if you have an /etc/rc.local file, you can put your commands in there.

michaelk 12-04-2014 07:41 PM

Possibly, since it is an embedded system may not have a rc.local

suicidaleggroll 12-04-2014 07:44 PM

Quote:

Originally Posted by michaelk (Post 5279728)
Possibly, since it is an embedded system may not have a rc.local

In my experience the usage of /etc/rc.local is about as hit or miss on embedded systems as it is on standard Linux distros. Either way, it's worth looking into. If it doesn't have one/can't use one, then put a custom script in /etc/init.d and link it into /etc/init.d/rc?.d, no harm no foul.

michaelk 12-04-2014 08:02 PM

True

django84 12-04-2014 09:34 PM

Guys, thanks a lot for your precious answers.
The camera starts in Run Level 3, so I went looking for rc3.d .
It contained these scripts:
S01respawnd S31ipsetd S41fpga S97ide.sh
S05sysklogd S33telnetd S87imgsrvd S98rick.sh
S20net.lo S35ftpd S90sync.sh S99launcher.php
S22net.eth0 S37ssh S92lighttpd
S30net.final S38net_mii S95usb

I created a sh script called S98user.sh, just between S97ide.sh and S99launcher.php .
The script is this:
Code:

#!/bin/sh                                                                                                             
# Startup script                                                                                               
                                                                                                                       
mount /dev/hda1 /mnt/0                                                                                                 

sleep 1                                                                                                               
echo "HDA1 mounted"                                                                                                   

mount /dev/hdb1 /mnt/1                                                                                                 

sleep 10                                                                                                               
echo "HDB1 mounted"                                                                                                   

alias snap4='/mnt/0/pics/snap4.sh'                                                                                   
alias snap5='/mnt/1/pics/snap5.sh'                                                                                   

echo "Aliases snap4 e snap5 created"

I don't know why but the alias section of the script doesn't seem to work: if I enter the cmd snap4 after the boot, it says that the cmd is not found.
But if I manually enter the cmd to create the alias, the cmd "snap4" is then recognized.
Do I have to put the alias section of the script in another specific folder?

rtmistler 12-05-2014 09:06 AM

@michaelk can you take a look at this other thread and let the OP know if anything's up they need to understand?

http://www.linuxquestions.org/questi...95#post5279995

suicidaleggroll 12-05-2014 09:57 AM

Quote:

Originally Posted by django84 (Post 5279771)
Guys, thanks a lot for your precious answers.
The camera starts in Run Level 3, so I went looking for rc3.d .
It contained these scripts:
S01respawnd S31ipsetd S41fpga S97ide.sh
S05sysklogd S33telnetd S87imgsrvd S98rick.sh
S20net.lo S35ftpd S90sync.sh S99launcher.php
S22net.eth0 S37ssh S92lighttpd
S30net.final S38net_mii S95usb

I created a sh script called S98user.sh, just between S97ide.sh and S99launcher.php .
The script is this:
Code:

#!/bin/sh                                                                                                             
# Startup script                                                                                               
                                                                                                                       
mount /dev/hda1 /mnt/0                                                                                                 

sleep 1                                                                                                               
echo "HDA1 mounted"                                                                                                   

mount /dev/hdb1 /mnt/1                                                                                                 

sleep 10                                                                                                               
echo "HDB1 mounted"                                                                                                   

alias snap4='/mnt/0/pics/snap4.sh'                                                                                   
alias snap5='/mnt/1/pics/snap5.sh'                                                                                   

echo "Aliases snap4 e snap5 created"

I don't know why but the alias section of the script doesn't seem to work: if I enter the cmd snap4 after the boot, it says that the cmd is not found.
But if I manually enter the cmd to create the alias, the cmd "snap4" is then recognized.
Do I have to put the alias section of the script in another specific folder?

Aliases are specific to the shell you are running. That script was launched in a subshell on boot, and as soon as the script was done the subshell exited and the aliases disappeared. If you want your user to have some alias, then you need to put it in that user's .bashrc, .bash_profile, .profile, etc. or in the system-wide /etc/profile, etc.

django84 12-05-2014 11:09 AM

Thanks suicidaleggroll for your answer. I forgot to mention in my previous post that I've already tried to include the alias cmds in other files, as you can see below.
I put the alias cmds in the shrc file and I created another file called alias.sh inside /etc/profile.d .

The etc/profile file is this :
Code:

ENV=/etc/shrc                                                                                                         
HOSTNAME=`hostname`                                                                                                   
                                                                                                                       
export ENV HOSTNAME

This is the etc/shrc I edited :
Code:

alias snap4='/mnt/0/pics/snap4.sh'                                                                                   
alias snap5='/mnt/1/pics/snap5.sh'                                                                                   
                                                                                                                       
for FILE in /etc/profile.d/*.sh ; do                                                                                   
                                                                                                                     
if [ -x $FILE ]; then                                                                                                   
                                                                                                                     
. $FILE                                                                                                                 
                                                                                                                     
fi                                                                                                                     
done                                                                                                                   
                                                                                                                       
unset FILE

Inside the dir /etc/profile.d/ there are two scripts: path.sh and prompt.sh.

As an example, path.sh is this:
Code:

PATH=/usr/local/bin:/usr/bin:/bin                                                                                     
[ $(id -u) -eq 0 ] && PATH="/usr/local/sbin:/usr/sbin:/sbin:$PATH"                                                     
export PATH

I tried to put a script inside the dir /etc/profile.d/ named alias.sh:
Code:

alias snap4='/mnt/0/pics/snap4.sh'                                                                                   
alias snap5='/mnt/1/pics/snap5.sh'

but aliases are not recognized yet.
How do I include the alias cmds in the /etc/profile file?
Do I have to just insert the cmds after the "export ENV HOSTNAME" line?

suicidaleggroll 12-05-2014 01:46 PM

Any of those should have worked. You will have to log out and back in to see anything, possibly even reboot the system.

django84 12-05-2014 03:02 PM

I always rebooted the system after these edits. With busybox it doesn't seem to be possible to log out though.
Do you think I can try to include the alias cmds just after the "export ENV HOSTNAME" line in the /etc/profile?

suicidaleggroll 12-05-2014 03:12 PM

Sure


All times are GMT -5. The time now is 08:07 AM.