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.

Community
  • 1
  • 1
sean
  • 1,632
  • 2
  • 15
  • 34

3 Answers3

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

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
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
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
  • Thanks. Could you explain for me why I should change "&>" to "| tee". What is the difference? – sean Dec 18 '14 at 06:59
  • `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. – Arnab Nandy Dec 18 '14 at 07:02
  • 1
    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. – sean Dec 18 '14 at 07:09
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.

Thushi
  • 188
  • 1
  • 13