Skip to content

Instantly share code, notes, and snippets.

@y56
Created April 9, 2020 01:35
Show Gist options
  • Save y56/e1645b6874e440ff991d57a94e9211f0 to your computer and use it in GitHub Desktop.
Save y56/e1645b6874e440ff991d57a94e9211f0 to your computer and use it in GitHub Desktop.
Ubuntu - Run command on start-up with “sudo”
Stack Overflow
Products
y56
13
, 13 reputation
●33 bronze badges
Dark mode beta
You’ve been asking for dark mode for years.
The dark mode beta is finally here.
Light
System
Dark
Change your preferences any time.
Home
Public
Stack Overflow
Tags
Users
Jobs
Teams
What’s this?
Free 30 Day Trial
Ubuntu - Run command on start-up with “sudo”
Ask Question
Asked 8 years ago
Active 3 years, 2 months ago
Viewed 107k times
91
I would like to run a sudo command when Ubuntu starts up (before anyone logs in):
sudo searchd
How would I do this?
linux ubuntu-11.10
shareedit
edited Sep 1 '14 at 16:48
blalasaadri
5,31544 gold badges3030 silver badges5353 bronze badges
asked Mar 13 '12 at 11:45
Mark Willis
1,60311 gold badge1313 silver badges2222 bronze badges
add a comment
2 Answers
Active
Oldest
Votes
194
You can add the command in the /etc/rc.local script that is executed at the end of startup.
Write the command before exit 0. Anything written after exit 0 will never be executed.
shareedit
edited Jan 12 '17 at 2:51
Nabin
8,16855 gold badges5050 silver badges8080 bronze badges
answered Mar 13 '12 at 11:47
Didier Trosset
30.9k1313 gold badges6969 silver badges106106 bronze badges
38
Before, for sure. Anything written after exit 0 will never be executed. – Didier Trosset Dec 6 '12 at 14:58
Hi I also have and rvmsudo command to be executed in same situation. the command normally would ask for password. Will there be any issues if I place the command directly in /etc/rc.local ? – ravi1991 Jul 9 '15 at 14:39
@fmonegaglia It depends. If you want the command to be started, and the script waits for it to finish before continuing to the next command on the script, then no &. If you want the following command to be started even if the current one is not finished, add a &. – Didier Trosset Jan 25 '16 at 9:12
@NoOne Check access rights on this file for the current user. Starting editor command with sudo may do the trick. – Didier Trosset Sep 23 '16 at 8:23
could you elaborate more? which program executes that script? seems like i don't have this file on a non-ubuntu (arch) distribution .. does ubuntu use another init system than systemd? or just some other config? – philx_x Feb 20 '17 at 20:13
show 4 more comments
9
Edit the tty configuration in /etc/init/tty*.conf with a shellscript as a parameter :
(...)
exec /sbin/getty -n -l theInputScript.sh -8 38400 tty1
(...)
This is assuming that we're editing tty1 and the script that reads input is theInputScript.sh.
A word of warning this script is run as root, so when you are inputing stuff to it you have root priviliges. Also append a path to the location of the script.
Important: the script when it finishes, has to invoke the /sbin/login otherwise you wont be able to login in the terminal.
shareedit
answered Mar 13 '12 at 11:57
Nugget
6,9991111 gold badges4141 silver badges7474 bronze badges
1
this answer was pretty useful for me when building our product appliance, thanks a lot – José F. Romaniello Oct 4 '13 at 16:59
add a comment
Your Answer
Links Images Styling/Headers Lists Blockquotes Code HTML
Advanced help
Community wiki
Not the answer you're looking for? Browse other questions tagged linux ubuntu-11.10 or ask your own question.
The Overflow Blog
Podcast 224: Cryptocurrency-Based Life Forms
Q2 Community Roadmap
Featured on Meta
Community and Moderator guidelines for escalating issues via new response…
Feedback on Q2 2020 Community Roadmap
Triage needs to be fixed urgently, and users need to be notified upon…
Dark Mode Beta - help us root out low-contrast and un-converted bits
Technical site integration observational experiment live on Stack Overflow
Linked
2
Running a php process on startup in Ubuntu
1
Automatically Start Multiple MySQL instances on boot in Ubuntu Trusty 14.04
0
How to configure ports on apache server for iperf3
2
Starting Nitrogen Web framework at boot time
Related
600
What killed my process and why?
1055
How to count lines in a document?
1331
How to exclude a directory in find . command
409
How to keep environment variables when using sudo
860
How to permanently set $PATH on Linux/Unix?
5147
How do I find all files containing specific text on Linux?
478
Cutting the videos based on start and end time using ffmpeg
Hot Network Questions
How can I disable address bar animation in Mozilla Firefox 75?
Should I insist on a refund or take Ryanair's offer to move travel date free of charge?
New decorated triangle for mathematical formulas
Does ground pour circumvent bypass capacitors?
Why not bring back the original actors for Maddox and Icheb in Star Trek: Picard?
Why does increasing altitude (low air pressure) results in lower resistivity/ breakdown voltage?
How to make sure non-open source programs are really using end-to-end encryption?
Expedient repair for water damaged drywall behind tile
What is the exact meaning of being at work?
Could you manually eject a floppy quick enough to prevent data loss?
Is a firewall enough of a security measure for an Ubuntu server that hosts a website?
Canadian citizen needs insulin in US during lockdown
more hot questions
Question feed
Stack Overflow
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
Products
Teams
Talent
Advertising
Enterprise
Company
About
Press
Work Here
Legal
Privacy Policy
Contact Us
Stack Exchange
Network
Technology
Life / Arts
Culture / Recreation
Science
Other
Blog
Facebook
Twitter
LinkedIn
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.4.8.36558
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment