Eject CD on shutdown

OK, so I have a simple problem: I have a live CD, and I want the CD to eject when the computer is rebooted or halted.

Executing /usr/bin/eject -m makes the CD eject just fine… trouble is, as you might expect, this then crashes the OS because it now can’t read any files. So the reboot never happens, because /sbin/reboot is inaccessible. (I tried executing reboot --help > /dev/null right before the reboot, but there are still other files which still can’t be read…)

In summary, it appears that I need to make the eject be the very last thing that happens. So it seems that systemd is the puppy I need to play with… But I have wasted literally hours of my life poking and prodding it, and no matter what I do, it never, ever, under any circumstances, actually ejects the CD. And I have no idea why. I’ve tried a dozen different ways of invoking eject, but nothing ever happens.

Can anybody tell me the simplest way to run eject? (I imagine just after umount.target would be sensible…)

Check out the scripts in /etc/init.d that are linked to in the runlevel directories /etc/rc0.d (halt) and /etc/rc6.d (reboot). For example, I put the eject command in /etc/init.d/reboot and it worked fine.

But, I’m not on a Live CD so I can’t test what would work.

If you think about it, something like this, it is hard to imagine, putting in a script or even a compiled binary. Even a binary requires some allocation/deallocation overhead, and as soon as you eject the CD, you are done. Rather, it should be like a flag and a pointer to a machine instruction, stored in some super-persistent memory…

Maybe it’s easier to just run the command and deliberately crash the OS, but to do so when almost all stuff is offline, so it won’t matter that you crash your remaining system.

(Edit) Another thought: Could you somehow partition your filesystem, creating a partition for just the eject command and reboot, unmount all the others, then run the command? (But, probably then fsck would have to be told not to complain every time, that the eject-partition was not unmounted correctly.) Or, could you put it on a USB stick? (This is a very interesting problem; if you solve it, do tell us!)

(Edit 2) What about merging eject and reboot, so you get one binary, it is loaded into memory, eject ejects the CD, but doesn’t have to load reboot from any filesystem, it is already in main memory?

Answered By: Emanuel Berg

The followings may help you :

  • Add your eject command at /etc/init.d/halt.local

  • You could also look at the /etc/init.d/skeleton template and
    customize this to perhaps add some output eg system shutting down,
    press any key to eject cdrom etc.

  • Check out the scripts in /etc/init.d that are linked to in the
    runlevel directories /etc/rc0.d (halt).

Answered By: Md Mahbubur Rahman

Eventually my colleague found the following article:


In summary, I created a file named /lib/systemd/system/eject.service which contains

Description=Eject the DVD

ExecStart=/usr/bin/eject -m


I also had to run systemctl enable eject.service to make this actually do anything. But now systemd ejects the CD right before shutdown, exactly as I wanted.

…unfortunately the OS still segfaults when it can’t find the rest of the files with the CD ejected. I tried adjusting the above file so we have After=unmount.target, which you would think runs the script after the filesystems have been unmounted. But no, still it segfaults.

It looks like I’m going to have to figure out how to copy the live CD image into RAM on boot – but that is an entire other question…

Answered By: MathematicalOrchid

ubuntu can use eject -m; reboot -f

Answered By: jamlee

Create a file named eject.shutdown with executable permissions in /usr/lib/systemd/system-shutdown/.

/usr/bin/eject -T

Source: System shutdown logic

See also: Eject CD before shutdown

Answered By: Steve Ward
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.