Need to run a .sh as root on boot or login

1,903

Solution 1

To get a file to execute at start up you essentially want to put the .desktop in /etc/xdg/autostart/

Here is how to set up a .desktop for your file from the command line:

sudo mv file.sh /usr/bin

This moves the shell file path to /usr/bin.

Then go to

/usr/share/applications

Nextyou want to create a .desktop file so you type

sudo -H gedit file.desktop & 

The & means you can still use the terminal but leave the file open. Handy if you have a goldfish memory like I do.

In gedit write:

[Desktop Entry]
Name=myfileName
Exec=/usr/bin/file.sh
Type=Application
Terminal=false

You can close it now if you like. You can jazz it up with an icon by adding the line icon=path/to/icon too if that takes your fancy.

Now to get this file to to run on start up you need to save a copy of it into

/etc/xdg/autostart/

That should work provided the script (myfile.sh) works already, that is.

Solution 2

You can try putting your file.sh in /etc/init.d/ and use update-rc.d

update-rc.d file.sh defaults

Solution 3

To run it on login (I am on 12.04, so this could be slightly different), simply click on the menu in the far top right (with the shutdown options) and click on Startup Applications...

Then click on Add, give it a name and description and browse to the location of the bash script. log out, then back on and it should work.

If you are doing it this way, I would suggest changing sudo to gksudo in your script so that it will then ask for sudo privileges in a nice GUI, rather than just sitting on the terminal asking for it.

Solution 4

I have an alternative suggestion to the other answers: use crontab

as root run crontab and add a job like so

@reboot /path/to/your/script

http://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions

This is how I would tackle this problem. I am not prepared to say it is better or worse than other answers and would welcome commentary.

Share:
1,903

Related videos on Youtube

Chris Hermut
Author by

Chris Hermut

Updated on September 18, 2022

Comments

  • Chris Hermut
    Chris Hermut over 1 year

    Is there a native method of DOM element in ECMAScript that will allow to count all ancestors of a given element (up to window object or DOM element specified by Id,Name etc.)?

    Example use is to check all ancestors of a given element and remove a specified attribute.

    • Magpie
      Magpie over 11 years
      Have you tried executing the file manually?
    • Graymayre
      Graymayre over 11 years
      I do, I want to find a way to automate it.
    • Magpie
      Magpie over 11 years
      Do you run that code in the terminal and need help turning it into a shell script or have you already a script? If the latter see my answer, if not I will try to extend it to help you with your script.
    • Ja͢ck
      Ja͢ck almost 10 years
      Nope, you'd have to write it yourself; jQuery has a .parents() or .parentsUntil() method for that.
    • Quentin
      Quentin almost 10 years
      Don't confuse parents and ancestors.
    • Chris Hermut
      Chris Hermut almost 10 years
      Ok. Thanks for the tip.
    • VLAZ
      VLAZ almost 10 years
      Not a direct in-built function, but if you get a hold of a DOM node, you can then access the parentElement property. You can then just recursively go. Something like var node = document.getElementById("myId"); and then while (node != null) { node = node.parentNode } would get you to the top
    • Admin
      Admin over 9 years
      The window object is not a DOM element. Perhaps you meant the html element.
  • Allfo
    Allfo over 11 years
    try using: sudo update-rc.d ae2500 defaults
  • Graymayre
    Graymayre over 11 years
    alright, done. this will run the scripts the same way?
  • Graymayre
    Graymayre over 11 years
    did a reboot and no luck, had to manually run it again
  • Allfo
    Allfo over 11 years
    When you moved your script file it will execute cd ~/ndiswrapper-1.58 which is now not found. Try moving ndiswrapper-1.58 directory to /etc/init.d/
  • Graymayre
    Graymayre over 11 years
    so ~ will default to that directory?
  • Graymayre
    Graymayre over 11 years
    gotcha. what ill do is delete the script, rewrite it to specify the directory instead of ~ and then it should work, right?
  • Chris Hermut
    Chris Hermut almost 10 years
    Sorry but I was asking for native Javascript solution.
  • VLAZ
    VLAZ almost 10 years
    Instead of keeping the ID and then hitting the DOM for each lookup (admittedly, not a huge hit, but still), you can keep the node and do while (node != null && node.id !== targetId) { /* your node manipulation */ node = node.parentNode; } Erm, yeah - taken (modified) from my comment to your question. I didn't see the answer you posted while I was posting the comment.
  • VLAZ
    VLAZ almost 10 years
    Actually, on second thought - what I said would be wrong - you are going the other direction - children instead of parents. My bad, I blame it on not enough caffeine. Still, I do think my approach is slightly better. The same principle applies - instead of looking up each child node by ID, you just traverse the DOM going currentNode = childNodes[i] It also solves the problem of potentially not having an ID for all nodes.
  • Chris Hermut
    Chris Hermut almost 10 years
    I was aware of that issue but didn't have time to actually modify that solution. Thanks for the tip will definitely use it.
  • Admin
    Admin over 9 years
    I don't know what you mean by "native JavaScript solution". document.createNodeIterator is just another DOM API like element.parentNode.
  • Admin
    Admin over 9 years
    As far as I can tell, this "correct" solution does not work at all. Besides the erroneous assumption that all DOM nodes have IDs, and the poor design which mixes finding the ancestor elements with performing some action on them, consider the case where the ending element has only one child. The loop will then terminate after one iteration, even if the ending element has not been reached. What is the point of consulting childNodes.length on the ending element, and looping over that?