autostart my helloworld application in openwrt - linux

I have created a simple HelloWorld application.
I want to autostart my application in OpenWRT(19.07.1) just after boot up.
The application should be started automatically after the shell prompt comes.
My helloworld application is in /usr/bin
I want to start this application automatically after bootup in openwrt
here is what I have created in /etc/init.d/script.sh
#!/bin/sh
START=10
start()
{
echo start
/usr/bin/helloworld
}
then
chmod +x script
/etc/init.d/script enable
After this I rebooted
I tried the above steps, but after rebooting no changes are reflected
manually I'm able to run my application.
Please help to resolve this issue.
Can anyone please write script for me??

Your shebang line,
#!/bin/sh /etc/rc.common
is wrong: it causes the shell to read and execute /etc/rc.common instead of the current file. Change it to
#!/bin/sh
instead, and it should now work.
If you have /usr/bin/whatnow:
#!/bin/sh /usr/local/bin/helpers
This part of the script will never be interpreted by the shell.
and /usr/local/bin/helpers:
#!/bin/sh
echo "This is the helper!"
then running whatnow will output This is the helper!.

Create a procd script as described in OpenWRT procd init script example. Place your script in /etc/init.d/<filename>.
It can look like this:
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=95
STOP=01
start_service() {
procd_open_instance
procd_set_param command /bin/sh "/var/myscript.sh"
procd_close_instance
}
There is many more options available in the manual.

Related

Running a bash script which automatically opens a terminal on bootup [duplicate]

This question already has answers here:
UBUNTU: XOpenDisplay(NULL) fails when program run in boot sequence via rc.local
(2 answers)
Closed 7 years ago.
I have to run a bash script on bootup which opens couple of terminals and runs some command in each terminal.
test.sh (My bash file)
#!/bin/bash
sleep 10
gnome-terminal --tab-with-profile="Default" -e 'bash -c '\''export TAB=1; mkdir /home/naman/Desktop/test_folder'\'
I have created a testjob.conf inside /etc/init/ :
testjob.conf (Upstart config file)
description "A test job file for experimenting with Upstart"
author "Naman Kumar"
start on runlevel [2345]
exec echo Test Job ran at `date` >> /var/log/testjob.log
exec bash /home/naman/Desktop/test.sh
Now, the problem is testjob.conf is not able to run the test.sh file on bootup (or it runs it but does not create a folder test_folder). If I remove the last line from testjob.conf : exec bash /home/naman/Desktop/test.sh, everything works and when I do cat /var/log/testjob.log, I get the correct output but if the last line is there, cat /var/log/testjob.log does not give the latest output.
I have also tried updating /etc/rc.local with : bash /home/naman/Desktop/test.sh but that also does not seem to be running the test.sh script on bootup
I am not sure whether they run the scripts but are not able to create the folder or they are not even able to run the script on bootup.
Note: I can not use System -> Preferences -> Startup Applications because I don't have any monitor, so the desktop application does not run. (I am running it on a Single Board Computer with no monitor).
Does anyone know what is the issue here and why is the test.sh script not running properly on bootup?
Thanks in advance.
Naman
Cong ma is pretty close on this. Sysvinit is for system level daemons. It has no idea for users. That means it doesnt have a way to interact with your window system (or gnome).
Further: without a monitor, what would you expect gnome-terminal to do? gnome-terminal would open up a terminal on the monitor: which you can't see.
What you should look at is taking your commands (date, etc) and putting them in /etc/rc.local and not trying to 'olay them into a different terminal or anything. Just literally run the commands there.

Run script automatically on startup: script doesn't work correctly

I want to run a bash script on startup of my Parallella board, which has Ubuntu. I searched in SO, and found the instruction in here:
Run automatically program on startup under linux ubuntu
Programmatically run at startup on Linux?
My bash script is test.sh, which has only one line:
echo "Hello World" &> /home/qsp/WIP/test/hello.txt
1) The first way I tried is adding to /etc/rc.local the aboslute path to the script:
/home/qsp/WIP/test/test.sh
2) The second way I tried is following the accepted answer above.
sudo mv test.sh /etc/init.d/
sudo update-rc.d test.sh defaults
In both cases, the script was executed after booting, and there was a file hello.txt created in the folder. However, the content of the file is empty (and the owner is root). I wonder if I'm missing anything. Thank you.
======UPDATE=======
Following the answer of Skynet, I change my script to:
echo "Hello World" | tee /home/qsp/WIP/test/hello.txt
and the script writes to the file after booting correctly. I have another question, why my original script with &> didn't work, although it still works if running from command line.
You should make it in init script style, as cited by the first SO question. Like so:
case "$1" in
start)
#startup code
;;
stop)
#stop code
;;
restart)
#restart code
;;
esac
Also take a look at https://github.com/fhd/init-script-template/blob/master/template
After editing /etc/rc.local and adding your commands,
check your script must always end with exit 0.
Also make it sure you made it executable by using chmod command
chmod 777 test.sh
Change the line of output as
echo "Hello World" | tee /home/qsp/WIP/test/hello.txt
Create .desktop file and configure your ystem to auto-start at the time of login
Create .desktop file as below
$ vim ~/.config/autostart/test_script.desktop
add the below information
[Desktop Entry]
Type=Application
Name=Test script
Exec=~/test.sh
X-GNOME-Autostart-enabled=true
Note that ~/test.sh should point to the script you've created. Save it.
Make it executable:
$ chmod o+x ~/.config/autostart/test_script.desktop
Reboot and for the next login your script should run.

Script in a embedded linux

I believe that this is a silly question but is my first time in wich I do a script. I am working on Linux, in an embedded system, and I think that what I want to do is quite simple but for me is not working.
I need to set an ip, start the startx & server for graphical mode and give to my application permission and run it, so I try like this:
#!/bin/sh
#
#Start
#
echo "Start......"
ifconfig eth0 X.X.X.X
startx &
cd /home
chmod a+x myApplication
./myApplication
exit $?
And then I save my script like S80script and I put it in the /etc/init.d folder.
I ran it but after throw the startx server my application is not run.
How can I do this in a propertly way?
There is another way for do this?
Thank you so much and sorry because maybe it is a beginner question.
If your application need to acess the XServer than you need to export the DISPLAY environment variable.
Try to run the application using:
DISPLAY=:0 ./myApplication
I would suggest you to install (for learning purposes) Linux on your laptop, and become familiar with Linux and scripting on your laptop. Then, replace during debugging phase the first line #!/bin/sh with #!/bin/sh -vx or #!/bin/bash -vx if you can run that script in a terminal. You could also use logger(1) in your script. Read the Advanced Bash Scripting Guide even if it is imperfect.
startx is configurable (read the linked man page), and is starting some client applications (configured in e.g. /etc/X11/xinit/xinitrc or in $HOME/.xinitrc...); so you should start your $HOME/myApplication from that file.
BTW, you could invoke startx in some init script like /etc/rc.local or whatever is appropriate for your Linux distribution.
BTW, you almost certainly need a window manager (to be started after your backgrounded application, as the last command, probably in the same xinitrc....).
At last, your embedded Linux distribution probably has some other files and scripts to start the network. You should configure your network parameters appropriately (on Debian and related, you could do that in /etc/network/interfaces)

Execute Script using an Alias

I am trying to create an alias that will execute a script. When i cd into the directory where the script is located... lets say /usr/local/bin/startscript then the script runs as expected and starts the application i want it to.
SO. i went into my bashrc file and added an alias
alias startscript='/usr/local/bin/startscript'
The goal is to be able to run the script by simply typing "startscript" from any directory.
However, when i try to use the alias to run the script, it does not work properly as the application that should start, does not.
My script starts with
#!/bin/sh
and then goes from there
any ideas? Thanks
SCRIPT:
#!/bin/sh
#- Check for user 'user'
if [[ "`whoami`" != "user" ]]; then
echo "This script can only be executed by user 'user'."; exit
fi
. /usr/local/bin/etctrx/startscriptdirectory/startscriptsetup
#- Kill manager to avoid multiple processes
pkill -f 'JavaApp.jar'
#- Start
nohup java -classpath /usr/local/bin/etctrx/startscriptdirectory/RequiredJars/ojdbc5.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/activation.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/mail.jar -jar /usr/local/bin/etctrx/startscriptdirectory/JavaApp.jar > ${JAVAAPPLOGS}/startscript.log 2>&1 &
If the script runs as expected while in /usr/local/bin by simply typing startscript, but from another directory the script runs (does not return an error), but doesn't produce the desired results, then the issue is with how you reference the application from within the script.
As others have noted, you shouldn't need an alias for something in /usr/local/bin and if it runs from that directory, obviously your executable permissions are correct too. If the application you're trying to run is also in /usr/local/bin then your script probably assumes it's in the same directory, which wouldn't be the case elsewhere, so you would need to either ad a cd to /usr/local/bin within the script or specify the full application path.
I am able to call the script if i do this, but it still won't give me the
results I want,(application being started) like i do when I run the script from
the directory it lives in
It would appear that the "application" in question is in the same directory as the script, /usr/local/bin, which we have established is already on your PATH. For the script to run correctly but not the application means you might be calling the application wrong, for example
./application
This would fail unless you were calling from /usr/local/bin. Fix would be like this
application

Tomcat script not working when run from Hudson

I'm trying to run a script which stops and starts Tomcat on linux.
When I run it from the command line it works fine. But it does not seem to work when I run the same script from the "Execute Shell" build step in a Jenkins/Hudson job. Jenkins doesn't report any errors but if I try going to the tomcat page then I get a page not found error.
So Jenkins seems able to stop the server, but not bringing it back up.
I'd be grateful for any help.
Try unsetting the BUILD_ID in your 'shell execute' block. You might even not need to use nohup in this case
BUILD_ID=
./your_hudson_script_that_starts_tomcat.sh
Without seeing your script it is difficult to give an exact answer. However you could try adding the following to the start of your script (assuming it is a bash script):
# Trace executed commands.
set -x
# Save stdout / stderr in files
exec >/tmp/my_script.stdout
exec 2>/tmp/my_script.stderr
You could also try adding
set -e
to make the shell exit immediately if a command returns an error status.
If it looks as though Hudson is killing off Tomcat then you might want to run it within nohup (if you're not already doing that):
nohup bin/startup.sh >/dev/null 2>&1 &

Resources