2

I often see some upstart script has the to use exec, what is the point? Seems removing the exec the script also work?

e.g.

start on runlevel [2345]
stop on runlevel [06]

exec /path/to/program
Ryan
  • 5,831
  • 24
  • 72
  • 91

2 Answers2

2

If you remove exec, then the startup script will continue running, waiting until the started utility terminates. E.g. inefficient and wastes extra RAM and other resources (unless you want to monitor some kind of abnormal termination, and restart the utility again)

With exec, instead of waiting for the started utility to terminate, the utility starts in place of an existing script.

cnst
  • 13,848
  • 9
  • 54
  • 76
  • While true that it's inefficient and wastes memory and resources, in practice, it's an insignificant amount. Unless you're deploying a script that will be used by thousands or millions of people, spending a few minutes thinking about it wastes more time and effort than it will ever save -- so if you don't understand the difference between using exec and just letting the script wait, just go with what you know. Otherwise you may end up wasting time debugging why the script doesn't "finish" running after the exec because you didn't know how exec worked. – Johnny Mar 26 '13 at 17:13
  • Some systems have process and file descriptor limits, so, if everyone starts leaving lots of old shells behind, some setups will quickly run out of available process IDs and file descriptors (and both of these resources are in the low hundreds by default on OpenBSD, for example). – cnst Mar 26 '13 at 17:20
  • I wrote my answer after having to clean up a bunch of start scripts that an admin "cleaned up" in the name of efficiency by using exec even when the script was supposed to be checking a return value - he didn't really know what exec did, he just knew it was more efficient. We couldn't figure out why a service was failing to start up without alerting us even though the script was clearly checking the return code -- until we discovered that he'd changed the scripts to use exec to start up the service. – Johnny Mar 26 '13 at 17:50
  • @Johnny, lol, yeap, modifying something without an understanding of what it does is a bad idea! That's why my answer already happens to have mentioned that exec is used when no abnormal termination has to be accounted for. – cnst Mar 26 '13 at 18:29
1

THere is a shell reading that script:

/path/to/program : will have the shell launch 'program' as a subcommand

exec /path/to/program : will replace the shell with 'program' (saving a pid, and other advantages. And it's fine : you no longer need the shell itself at that point, as there is no further things for that shell to do)

Olivier Dulac
  • 1,202
  • 7
  • 14