对于一个成功执行的进程,是否可以使用非零的返回代码?

时间:2022-08-29 14:45:32

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".

我能找到的最简单的方法是退出,以动作的数量作为返回代码。例如,该进程将退出并返回“执行的3个操作”的代码3。

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?

但是,标准(AFAIK)是当一个进程成功退出时使用返回码0,当出现错误时,它会使用任何其他值,这种方法是否会产生任何问题?


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

注意:子进程不是可执行脚本,而是父进程的分支,因此不能从外部世界访问。

4 个解决方案

#1


2  

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.

常规程序员不应该违反返回约定更改。

#2


1  

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.

唯一的风险是混淆调用脚本。你所描述的是有道理的,因为你真正想要的是伯爵。正如Joe所说,对失败使用负值,您应该考虑包含一个-help选项来解释返回值……因此,当你下个月尝试使用这些代码时,你可以知道它们在做什么。

#3


0  

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...

我不会更改返回约定……

#4


0  

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.

如果调度器跨越一个子进程,并且您正在编写,那么您也可以为每个子进程打开一个管道,或者一个命名管道,或者可能是unix域套接字,并使用它进行进程间通信并在那里编写处理后的作业。

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.

我将坚持惯例,即为成功返回0,特别是如果您的程序被其他人看到/使用,或者无论如何要记录好这些决策。

Anyway apart from conventions there are also standards.

不管怎样,除了传统,还有标准。

#1


2  

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.

常规程序员不应该违反返回约定更改。

#2


1  

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.

唯一的风险是混淆调用脚本。你所描述的是有道理的,因为你真正想要的是伯爵。正如Joe所说,对失败使用负值,您应该考虑包含一个-help选项来解释返回值……因此,当你下个月尝试使用这些代码时,你可以知道它们在做什么。

#3


0  

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...

我不会更改返回约定……

#4


0  

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.

如果调度器跨越一个子进程,并且您正在编写,那么您也可以为每个子进程打开一个管道,或者一个命名管道,或者可能是unix域套接字,并使用它进行进程间通信并在那里编写处理后的作业。

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.

我将坚持惯例,即为成功返回0,特别是如果您的程序被其他人看到/使用,或者无论如何要记录好这些决策。

Anyway apart from conventions there are also standards.

不管怎样,除了传统,还有标准。