Need to run a .sh as root on boot or login
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.
Related videos on Youtube
Chris Hermut
Updated on September 18, 2022Comments
-
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 over 11 yearsHave you tried executing the file manually?
-
Graymayre over 11 yearsI do, I want to find a way to automate it.
-
Magpie over 11 yearsDo 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 almost 10 yearsNope, you'd have to write it yourself; jQuery has a
.parents()
or.parentsUntil()
method for that. -
Quentin almost 10 yearsDon't confuse parents and ancestors.
-
Chris Hermut almost 10 yearsOk. Thanks for the tip.
-
VLAZ almost 10 yearsNot 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 likevar node = document.getElementById("myId");
and thenwhile (node != null) { node = node.parentNode }
would get you to the top -
Admin over 9 yearsThe window object is not a DOM element. Perhaps you meant the
html
element.
-
-
Allfo over 11 yearstry using: sudo update-rc.d ae2500 defaults
-
Graymayre over 11 yearsalright, done. this will run the scripts the same way?
-
Graymayre over 11 yearsdid a reboot and no luck, had to manually run it again
-
Allfo over 11 yearsWhen 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 over 11 yearsso ~ will default to that directory?
-
Graymayre over 11 yearsgotcha. what ill do is delete the script, rewrite it to specify the directory instead of ~ and then it should work, right?
-
Chris Hermut almost 10 yearsSorry but I was asking for native Javascript solution.
-
VLAZ almost 10 yearsInstead 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 almost 10 yearsActually, 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 almost 10 yearsI was aware of that issue but didn't have time to actually modify that solution. Thanks for the tip will definitely use it.
-
Admin over 9 yearsI don't know what you mean by "native JavaScript solution".
document.createNodeIterator
is just another DOM API likeelement.parentNode
. -
Admin over 9 yearsAs 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?