I'm new to Linux. I wrote a very simple script to shutdown my computer, written as so:

echo "Hello World"
sudo shutdown

When I execute this code, it starts a shutdown schedule, usually a minute or so ahead, even though my script does not provide for scheduling, instead I want to immediately shutdown my system. I would like to know if this a function of my script or of Linux, and what I can do to fix my script so that it shuts down instantly.

  • 4
    Did you read the fine manual (man shutdown) in particular the section explaining the optional [TIME] argument? also !# should be #! – steeldriver Jul 1 '17 at 22:46

Best thing to do is type each of your commands in the terminal to test what the command will do. In your case, I think you want to use the following:

shutdown now
| improve this answer | |
  • 1
    This could be a good answer with some work. For example: Why does trying the commands one at a time offer an advantage? What is the difference between sudo shutdown and shutdown now? – hildred Jul 1 '17 at 23:38

There are three issues you need to address in your script.

The first is the typo in the shebang line. the correct syntax for such a line would be #!/path/to/interpreter -options and arguments. It must begin in the first character of the first line and ends with a newline. bash is an acceptable shell for scripting (although it might be overkill in this case) so after fixing the first two characters it would be acceptable, but a couple options may be of use. -e exits on any error forcing you to write better scripts and -x shows what the shell is doing at every step although neither would have caught the issues in this case.

The two most common mistakes with shutdown are time and kind of shutdown, and you seem to have hit both of them. Shutdown's main feature is to shut down at a time. There are three ways to specify a time absolute, relative, and now. Now is an alias of +0 which means zero seconds from now and is most often what is wanted on single user systems. Shutdown's other main feature is that it supports multiple types of shutdowns for example halt, poweroff and reboot, but because shutdown predates automatic power management the default is to shut down to a single user mode. It used to be used so that after you kicked all the users off you could sync, unmount and check filesystems before actually powering the system down. It was also useful for doing other maintenance before switching back to a multiuser mode. It is not much used these days but is still the default. The man page will tell you how to select the type of shutdown.

Also sudo is not needed for the root user so you could skip it for the root user.

| improve this answer | |
  • You might want to provide a real world example of a shebang like, ie #!/bin/bash – roaima Jul 2 '17 at 0:09
  • @roaima, steeldriver already did, besides If steeldriver's comment didn't cut it then the why is more important than the how. – hildred Jul 2 '17 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.