Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a running executable in linux

Tags:

linux

I have an embedded linux system that can update itself from a USB card. The interface program detects the USB insertion and looks for the upgraded executable. I currently copy it to a local file and install some commands in rc5.d to copy the file over the existing exe on the next boot. Then I have the software reboot.

Is there a better way to do this?

like image 986
David Huff Avatar asked Nov 10 '09 23:11

David Huff


People also ask

Can you update Linux while running?

The upgrade can be run while the program runs, but the running program you see is actually the old version of it. The old binary remains on the disk until you close the program. Explanation: on Linux systems, a file is just an inode, which can have several links to it.

Where are the executable files in Linux?

Please note that executables can also be located in /opt//bin and in /opt//sbin. Further, there are often in /usr/libexec also. Show activity on this post. Note that you can execute from virtually any location, e.g. Documents, as long as the x bit is set and you have access.

What is the executable on Linux?

The standard Linux executable format is named Executable and Linking Format ( ELF). It was developed by Unix System Laboratories and is now the most widely used format in the Unix world.


1 Answers

You don't need to have it copy the file over on next boot. Instead, this sequence will work fine:

  • Copy new executable to a local file.
  • Verify local file.
  • unlink() existing executable.
  • rename() new executable to the correct name.

The application will keep running after the unlink() - the kernel won't release the underlying data until all executing copies are finished.

You can then even just use execve() to have the currently-running process replace itself with the newly uploaded version.

like image 104
caf Avatar answered Oct 04 '22 19:10

caf