2

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:

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.

1
  • Check this one too Commented Dec 18, 2014 at 6:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

Thanks. Could you explain for me why I should change "&>" to "| tee". What is the difference?
tee is a command which work similarly as shift does but different way which make it usable in conditions like in this case where the redirection is being happening within an shell script.
Thanks. After changing to tee, the script writes to the file correctly. I wonder why &> doesn't work after booting, while it still works if I just run it from command line.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.