BenMorel
BenMorel

Reputation: 36554

Is is OK to use a non-zero return code for a process that executed successfully?

I'm implementing a simple job scheduler, which spans a new process for every job to run. When a job exits, I'd like it to report the number of actions executed to the scheduler.

The simplest way I could find, is to exit with the number of actions as a return code. The process would for example exit with return code 3 for "3 actions executed".

But the standard (AFAIK) being to use the return code 0 when a process exited successfully, and any other value when there was en error, would this approach risk to create any problem?


Note: the child process is not an executable script, but a fork of the parent, so not accessible from the outside world.

Upvotes: 2

Views: 112

Answers (4)

DRC
DRC

Reputation: 5048

If the scheduler spans a child and you are writing that you could also open a pipe per child, or a named pipes or maybe unix domain sockets, and use that for inter process communication and writing the processed jobs there.

I would stick with conventions, namely returning 0 for success, expecially if your program is visible/usable around by other people, or anyway document well those decisions.

Anyway apart from conventions there are also standards.

Upvotes: 0

drahnr
drahnr

Reputation: 6886

What you are looking for is inter process communication - and there are plenty ways to do it:

  • Sockets
  • Shared memory
  • Pipes
  • Exclusive file descriptors (to some extend, rather go for something else if you can)
  • ...

Return convention changes are not something a regular programmer should dare to violate.

Upvotes: 2

Steve
Steve

Reputation: 716

The only risk is confusing a calling script. What you describe makes sense, since what you want really is the count. As Joe said, use negative values for failures, and you should consider including a --help option that explains the return values ... so you can figure out what this code is doing when you try to use it next month.

Upvotes: 1

Alberto Megía
Alberto Megía

Reputation: 2255

I would use logs for it: log the number of actions executed to the scheduler. This way you can also log datetimes and other extra info.

I would not change the return convention...

Upvotes: 0

Related Questions