ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to have my home server shutdown every day at a certain time. Today I log into it and issue the shutdown command, but I would like this to be done automatically.
My thought was to write a simple shellscript (shutdown -h 22:30) and have cron run it.
The problem is that only root can initiate shutdown.
Can I solve with with a script, and what needs to be done, or is there a better way of accomplishing this?
Why a script to issue a system command? I've never tried shutdown, but can't think of a reason the following wouldn't work. Just add this line to root's crontab.
The problem is that only root can initiate shutdown.
Can I solve with with a script, and what needs to be done, or is there a better way of accomplishing this?
No, this can not be solved with a script: Then the script has to be executed by root.
if you add the line:
Code:
twallstrom ALL=NOPASSWD: /bin/cat
to /etc/sudoers, then the user "twallstrom" will be able to run the 'shutdown' command as root by running "sudo /sbin/shutdown -h 22:30"
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
The cron solution seems the best to me. I shut down all workstations in my home every night using that line.
However, I think Weibullguy made a mistake. The line he gave is to be entered in /etc/crontab. When it is in the root's crontab the user name should not be specified. Entering this line in /etc/crontab is the preferred way, because it is readable by all so you can see what happens even when you are an ordinary user.
Don't forget to put an additional blank line in crontab after the last line otherwise it won't work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.