我正在尝试编写一个模拟计算器的简单程序。我希望程序在按下Ctrl+D键时退出或关闭。我搜索了 stackoverflow 并看到了Ctrl+C或Ctrl+ 的其他示例,A但这些示例是在 java 和 C 中的。
对于 C:
(scanf("%lf", &var);
Run Code Online (Sandbox Code Playgroud)
对于 java,按下+SIGINT时会引发a 。 CtrlZ
signal(SIGINT,leave);
for(;;) getchar();
Run Code Online (Sandbox Code Playgroud)
我想知道我可以为C++ 中的Ctrl+做什么D...
谢谢大家!
从 bash,我想在新终端中启动一个新进程,等到它完成,然后恢复退出代码。
例如,我想做这样的事情,但实际上恢复有用的退出代码;
gnome-terminal -e "bash -c \" sleep 2s ; if [ '1' == '1' ] ; then exit 2 ; else exit 3 ; fi \""
echo gnome terminal returns $?
#gnome terminal returns 0
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
我想获得 bash 提供的相同功能,set -e但在 Windows 批处理文件中。这怎么可能?
目前这允许我退出,但我不想在批处理文件的每一行之后添加这一行。
IF %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL%
Run Code Online (Sandbox Code Playgroud) 我在基于 *nix 的操作系统上运行,并且有一个脚本可以同时启动多个进程。我的主要目标是同时启动这些进程,并收集每个进程返回的退出状态。我发现 usingwait(pid)可以实现这一点,因为所有子进程都归父进程所有。但是,我担心一旦子进程(启动的并发进程之一)完成,其 PID 将被释放并可供系统内回收。
所以我想问题是,如果一个父进程同时启动多个子进程,是否会在父进程完成之前将完成的子进程的 PID 提供给系统以进行回收?如果是这样,我怎样才能最好地获得每个子进程的退出状态?
下面的 bash 脚本示例:
local file=$1
local count=0
<files are split; and suffixed with aa,ab,ac,ad>
/home/text/concurrencyTest.sh $file-aa >> /home/text/$file-aa.log 2>&1 &
/home/text/concurrencyTest1.sh $file-ab >> /home/text/$file-ab.log 2>&1 &
/home/text/concurrencyTest2.sh $file-ac >> /home/text/$file-ac.log 2>&1 &
/home/text/concurrencyTest3.sh $file-ad >> /home/text/$file-ad.log 2>&1 &
for job in `jobs -p`
do
echo "Job: $job"
wait "$job"
rc=$?
echo "RC for $job is $rc"
if [[ rc -ne 0 ]]; then
FAIL[$count]="$job"
((count++))
fi
done …Run Code Online (Sandbox Code Playgroud) 任何命令行应用程序都会返回代码作为退出代码,通知主机环境指令的最终状态。VS命令行:devenv.com返回代码数量。尽管没有(或者我找不到任何)与此相关的此类列表或帖子。有任何文档吗?如果没有,VS 团队的任何人都可以在这里记录这一点吗?
我有一个 zip 文件,它在 Windows 服务器上压缩并具有 Windows 目录结构。
\n\n我正在尝试使用以下命令在 ubuntu 服务器上解压缩该文件:
\n\n/tmp/temp \xc2\xbb unzip -d /tmp/temp/temp backup.zip \nRun Code Online (Sandbox Code Playgroud)\n\n解压工作正常,目录结构自动转换为基于 Linux 的目录结构。但由于某种原因,extract 命令的退出代码为 1,这扰乱了我的 Jenkins 工作,该工作无法进一步进行。
\n\n inflating: /tmp/temp/temp/fs/site/wwwroot/vendor-a2341eb904.js \n inflating: /tmp/temp/temp/fs/site/wwwroot/vendor-fc433e18b6.css \n inflating: /tmp/temp/temp/fs/site/wwwroot/web.config \n inflating: /tmp/temp/temp/meta \n------------------------------------------------------------\n/tmp/temp \xc2\xbb echo $? \n1\nRun Code Online (Sandbox Code Playgroud)\n\n我什至测试了存档,将提取的文件的 CRC 与存档中的 CRC 进行比较,结果似乎没问题:
\n\n testing: fs\\site\\wwwroot\\vendor-fc433e18b6.css OK\n testing: fs\\site\\wwwroot\\web.config OK\n testing: meta OK\nNo errors detected in compressed data of backup.zip.\n------------------------------------------------------------\n/tmp/temp \xc2\xbb echo $? \n0\nRun Code Online (Sandbox Code Playgroud)\n\n知道 unzip 命令中返回代码 1 的原因是什么吗?
\n\n顺便说一句:我还尝试set …
我最近了解到关闭 Spring Boot 应用程序的正确方法是:
public class Application {
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return new ExitCodeGenerator() {
@Override
public int getExitCode() {
return 0;
}
};
}
public static void main(String[] args) throws Exception {
System.exit(SpringApplication.exit(SpringApplication.run(Application.class, args)));
}
}
Run Code Online (Sandbox Code Playgroud)
这应该返回退出代码 0,或者我将其配置为在getExitCode()方法中返回的任何内容。我的问题是 - 执行上述方法与执行以下方法有什么区别:
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
两种方法似乎都以完全相同的方式关闭应用程序,至少在控制台中是这样。那么有什么区别呢?
我有一些 powershell 脚本,当它们内部出现故障时,我试图将它们作为 Windows 任务调度程序中的失败状态触发。所以我在powershell脚本中做这样的事情。我尝试了 1 或 99 的退出代码,但看起来 Windows 任务调度程序并未将其视为失败状态。所以我的失败代码电子邮件不会发送出去通知我。
如何让任务调度程序看到我的 powershell 脚本失败?它总是有 129(创建的任务流程)、100(任务开始)、200(动作开始)、110(任务触发)、201(动作完成)、102(任务完成)的事件代码。
$global:ErrorStrings = New-Object System.Collections.Generic.List[System.Object] #I add strings onto the list as I find errors
$errorCodeAsString = ""
foreach ($item in $global:ErrorStrings.Members){
$errorCodeAsString += (" " + $item + "..")
}
if($errorCodeAsString -ne "")
{
write-output "Error: $errorCodeAsString"
Exit 99 #Exit 1 didn't cause task scheduler to see error at exit either
}
Exit 0
Run Code Online (Sandbox Code Playgroud)
我知道我的列表中充满了错误,因为我创建了它们来测试它。我检查了作为字符串的 errorCode 是一个长度并点击了退出 99 或 1。任务调度程序仍然显示正常的事件代码。
我有一个关于计划失败的电子邮件警报,由于事件代码没有显示失败,它永远不会触发发送我的电子邮件。这是 Windows 10,以防万一。
我一直在查看powershell …
我有两个可执行文件:prog1和prog2.
这些可执行文件读取输入数据,然后对其进行一些计算。根据输入数据的“类型”,必须使用prog1或。prog2
我想为只需要调用 的用户简化这一点prog,这是一个 shell 脚本,
prog1),prog1或prog2与exec.对于串行程序(无 MPI),这种方法效果很好。对于 MPI,我想prog使用mpirun -n 2 prog. 然后,两个进程调用 shell 脚本。如果步骤 (1) 涉及运行另一个 MPI 可执行文件,则此操作会失败。
一个简单的解决方案是编译prog1(用于步骤 1)的串行版本并使用此版本。然而,如果我可以使用已有的 MPI 可执行文件,那么对我来说会简单得多。
下面,我给出一个非常简单的测试用例。
我在互联网上发现了类似的问题,但没有一个真正对我有帮助。
最接近的一个来自英特尔论坛: https://software.intel.com/en-us/forums/intel-clusters-and-hpc-technology/topic/754976 (不幸的是没有回复)
这是一个最小的 MPI 程序 ( run.f90):
program run
use mpi
implicit none
integer::num_process,rank,ierr;
call MPI_Init(ierr);
call MPI_Comm_rank(MPI_COMM_WORLD, rank,ierr);
call MPI_Comm_size(MPI_COMM_WORLD, num_process,ierr);
write(*,*) 'I am',rank
call MPI_Finalize(ierr);
end …Run Code Online (Sandbox Code Playgroud) 每当启动新的 PowerShell 进程时,Ansgar Wiechers 的答案都很有效。/sf/answers/3514186441/这适用于 cmd.exe 和 powershell.exe。
C:>type .\exit1.ps1
function ExitWithCode($exitcode) {
$host.SetShouldExit($exitcode)
exit $exitcode
}
ExitWithCode 23
Run Code Online (Sandbox Code Playgroud)
在 cmd.exe 交互式 shell 中。
C:>powershell -NoProfile -Command .\exit1.ps1
C:>echo %ERRORLEVEL%
23
C:>powershell -NoProfile -File .\exit1.ps1
C:>echo %ERRORLEVEL%
23
Run Code Online (Sandbox Code Playgroud)
在 PowerShell 交互式 shell 中。
PS C:>powershell -NoProfile -Command .\exit1.ps1
PS C:>$LASTEXITCODE
23
PS C:>powershell -NoProfile -File .\exit1.ps1
PS C:>$LASTEXITCODE
23
Run Code Online (Sandbox Code Playgroud)
但是...在现有交互式 PowerShell 主机内运行 .ps1 脚本将完全退出主机。
PS C:>.\exit1.ps1
<<<poof! gone! outahere!>>>
Run Code Online (Sandbox Code Playgroud)
如何阻止它退出主机 shell?
exit-code ×10
linux ×2
powershell ×2
return-code ×2
bash ×1
batch-file ×1
c++ ×1
cmd ×1
command-line ×1
concurrency ×1
devenv ×1
mpi ×1
pid ×1
sh ×1
spring ×1
spring-boot ×1
unzip ×1
windows ×1