Run a command line then shut down when it finishes
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Run a command line then shut down when it finishes
I want to run a wipe on a hard drive before I give it away, but I don't want to wait around for it to finish before I turn off my computer. How could I run the wipe command then when that completes have my computer shutdown -h now?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Make a little shell program with the commands in it, make it executable (chmod 755 progname) and run it as root.
When you run the program it will execute whatever wipe you're using and when that finishes it will execute the next line (your shutdown, which could also be init 0).
When you're wiping the disk you'll have enough system left in RAM to do the job.
However, you don't need to wipe it (as in walking directories and the like), just reformat the thing, much faster.
dd if=/dev/zero of=/dev/sdx bs=1M && shutdown -h now
Will this suffice?
I'm not sure what "dd" will do once it gets to the end of the drive. Will it exit with success or fail for an exit code? You'll catch any errors that occur and that is good, but it is possible that running to the end of the drive will also be considered an error and the shutdown aborted. Making the shutdown unconditional would fix that, but then any error messages would be lost:
Code:
dd if=/dev/zero of=/dev/sdx bs=1M; shutdown -h now
Last edited by Turbocapitalist; 09-18-2016 at 10:49 PM.
Reason: reduced risk for copy-paste
While you're at it, may as well be aware of what dd reported (if there were errors) as shutting down will make those go away.
Code:
# dd if=/dev/zero of=/dev/sdx bs=1M &> ~/dd_wiped_sda.log; shutdown -h now
Goes without saying this is a very bad command to copy and paste right?
OP, please triple check the directory /dev/sdx before running any of the commands posted here
Thanks everybody. that's a good idea to have it actually log so I can see what it did. so is dd the best way to wipe your data so it can't be recovered. I see examples online for dd, wipe, and some other stuff.
There are some pretty extreme opinions on secure erasing, but I'd say up 3 'dd' passes should be plenty eg all zeros, all 1's, random (choose your own order).
'shred' is pretty good but I think (?) it only does existing dirs + files.
Have a google around before deciding; you could even use more than one method.
While you're at it, may as well be aware of what dd reported (if there were errors) as shutting down will make those go away.
Code:
# dd if=/dev/zero of=/dev/sdx bs=1M &> ~/dd_wiped_sda.log; shutdown -h now
Goes without saying this is a very bad command to copy and paste right?
OP, please triple check the directory /dev/sdx before running any of the commands posted here
Which is why I got into the habit of posting something like:
Code:
# dd if=/dev/zero of=/dev/your_device_name_here_replace_this bs=1M &> ~/dd_wiped_sda.log; shutdown -h now
Then explain what they're to replace that verbose non-sensical device name with.
If the person copying and pasting (we know it's a bad idea but that doesn't exactly stop people, does it?) copies the above with sdx, if there happen to be an "sdx", it's toast.
Thanks all. I've run in to a problem with dd though, apparently if it hits a bad spot on the drive, it totally dies rather than just reporting it and moving on. unless I'm missing a switch or something to make that happen.
How about then creating a large file, then deleting it, before shutting down? Granted the filesystem will still be in place, but the actual data within the filesystem will be gone. The fs should be able to compensate for those bad blocks.
The only switch I can find for dd to ignore a bad block is on reading.
I couldn't find a ignore on write either, but I wouldn't put it past me to miss a switch reading a man page haha
Yeah, might have to go the big file/delete/big file/delete... method, that's the only thing I could think of too
Thanks all. I've run in to a problem with dd though, apparently if it hits a bad spot on the drive, it totally dies rather than just reporting it and moving on. unless I'm missing a switch or something to make that happen.
Thanks all. I've run in to a problem with dd though, apparently if it hits a bad spot on the drive, it totally dies rather than just reporting it and moving on. unless I'm missing a switch or something to make that happen.
While I agree that it's dangerous to do this ... using dd conv=noerror should allow it to continue.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.