我有一个 python 文件 a.py。我正在使用从文件返回sys.exit(some_val)。
现在,我又多了一个文件 b.py,我从中执行 a.py,如下所示:
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
output, error = proc.communicate()
Run Code Online (Sandbox Code Playgroud)
sys.exit()您能否告诉我是否有任何方法可以从 b.py 获取值(使用 a.py返回)?
我在 IntelliJ 中有一个 Spring Boot 应用程序,最初配置为正常启动,如下所示:
public static void main(String[] args) throws Exception {
SpringApplication.run(AccountApiApplication.class, args).close();
}
Run Code Online (Sandbox Code Playgroud)
我在方法中添加了一个 try-catch 块main来处理启动期间发生的任何错误(例如缺少配置文件等),现在看起来像:
public static void main(String[] args) throws Exception {
try {
SpringApplication.run(AccountApiApplication.class, args).close();
}
catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
Run Code Online (Sandbox Code Playgroud)
添加此后,我的应用程序总是以退出代码 1 退出。即使没有错误也是如此。我尝试打印发生的异常,它是这样的:
org.springframework.boot.devtools.restart.SilentExitExceptionHandler$SilentExitException
at org.springframework.boot.devtools.restart.SilentExitExceptionHandler.exitCurrentThread(SilentExitExceptionHandler.java:90)
at org.springframework.boot.devtools.restart.Restarter.immediateRestart(Restarter.java:184)
at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:163)
at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:552)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)
at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at edu.iu.es.ebs.AccountApiApplication.main(AccountApiApplication.java:90)
Process finished with exit …Run Code Online (Sandbox Code Playgroud) 我有一个包含多个作业的 jar,我想每次只执行一项作业并检索自定义退出代码。
例如,我有一个基本的作业 ( retrieveErrorsJob) 配置,其中一个步骤将读取输入的 XML 文件并将数据写入特定的数据库表中。
应用类
@SpringBootApplication
@EnableBatchProcessing
@Import(CoreCommonsAppComponent.class)
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
private ConfigurationConstants constants;
@Autowired
public Application(ConfigurationConstants constants) {
this.constants = constants;
}
@EventListener(ApplicationStartedEvent.class)
public void idApplication()
{
logger.info("================================================");
logger.info(constants.APPLICATION_NAME() + "-v." + constants.APPLICATION_VERSION() + " started on " + constants.REMOTE_HOST());
logger.info("------------------------------------------------");
}
public static void main(String... args) throws Exception{
ApplicationContext context = SpringApplication.run(Application.class, args);
logger.info("================================================");
SpringApplication.exit(context);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以从命令行选择一项工作:
java -jar my-jar.jar --spring.batch.job.names=retrieveErrorsJob --input.xml.file=myfile.xml
Spring Batch …
我似乎无法设置 dotnet 核心控制台应用程序的退出代码,以便在 RHEL7 上观察到预期的行为。我正在使用 dotnet 2.0.3。
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Environment.Exit(12345);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 Windows 上,执行程序时错误级别设置正确
dotnet HelloWorld.dll
echo %errorlevel%
12345
Run Code Online (Sandbox Code Playgroud)
在 RHEL7 上,这不是所见
dotnet HelloWorld.dll
echo $?
57
Run Code Online (Sandbox Code Playgroud)
我尝试了各种方法来返回退出代码(设置 Environment.ExitCode 属性,从程序中返回一个整数)但都没有成功。
我将尽快尝试对最新的 dotnet 核心版本 (2.1.302) 进行测试,但同时发布此问题。
有谁知道这里发生了什么?
谢谢
这个stackoverflow 答案解释了 Linux 真正的命令是什么。我的问题是,Powershell (v5/v6) 是否也提供真正的命令?
我已经用谷歌搜索过Get-help *true*,但找不到任何有关它的信息。
谢谢
我在 bash 中有这个函数,它使用 netcat 检查某个地址上的端口是否打开。例如,我正在使用 localhost 3000 进行测试。
错误
我无法从运行中获取错误代码nc。我一直看到消息:
nc_output:预期的整数表达式
从 if 条件。(见下面的代码)
代码
#!/bin/bash
set -o nounset
scan() {
local host=$1
local port=$2
local nc_output
nc_output=$(nc ${host} ${port} -w 5)
eval ${nc_output}
if [ nc_output -ne 0 ]; then
"PORT ${port} CLOSED on ${host}"
else
echo "PORT ${port} OPEN on ${host}"
fi
}
scan 127.0.0.1 3000
Run Code Online (Sandbox Code Playgroud) 我希望能够在PowerShell中运行的命令提示符脚本(即)的开头将%ERRORLEVEL%环境变量(也称为“系统变量”)设置为任意值。每当人们想要设置(1)时,他们都会执行以下操作:cmd /c%ERRORLEVEL%
cmd /c "exit /b 3"
cmd /c "echo %ERRORLEVEL%"
Run Code Online (Sandbox Code Playgroud)
但是,尽管在正常命令提示符中运行上述命令时%ERRORLEVEL%设置为3,但如果在 PowerShell 中执行这些确切的行,则退出代码环境变量不会设置为3并保持为0。此外,您不能执行以下操作:
cmd /v:on /c "exit /b 3 & echo !ERRORLEVEL!"
Run Code Online (Sandbox Code Playgroud)
该exit命令完全脱离命令执行(即),并且在运行cmd /c后没有其他命令。&
因此,我尝试在PowerShell中执行以下命令:
cmd /v:on /c "SET %ERRORLEVEL% = 4 & echo !ERRORLEVEL!"
Run Code Online (Sandbox Code Playgroud)
预期输出是4,但这总是输出0。我不明白为什么我无法使用SET环境%ERRORLEVEL%变量。我已经使用了延迟命令执行(示例在这里),但这里似乎没有任何摆弄。
有谁知道为什么该命令SET %ERRORLEVEL% = …
powershell cmd exit-code errorlevel delayedvariableexpansion
在这段代码中:
echo hello > hello.txt
read X <<< $(grep hello hello.txt)
echo $?
Run Code Online (Sandbox Code Playgroud)
$?指的是读取语句的退出代码,它是 0。有没有办法知道grep失败(例如,如果hello.txt已被另一个进程删除)而不将read和拆分为grep两个语句(即,先grep检查$?然后检查read)。
我们正在使用以下脚本在 Windows 服务器上安装 MSI 文件,并且能够在 Windows 服务器上安装 MSI 文件。以下代码对于某些 MSI 文件运行良好,但对于其他文件则失败。退出代码为 1603。如果我们进行全新安装,它工作正常,但在尝试重新安装时,我们收到退出代码:1603 错误。所有服务的所有配置设置都相同。
正如Microsoft 网站上提到的,我们验证了以下条件,但没有一个条件适用于我们的案例。
Windows Installer 正在尝试安装您的电脑上已安装的应用程序。
您尝试安装 Windows Installer 程序包的文件夹已加密。
包含您尝试安装 Windows Installer 程序包的文件夹的驱动器将作为替代驱动器进行访问。
SYSTEM 帐户对您尝试安装 Windows Installer 程序包的文件夹没有完全控制权限。您会注意到该错误消息,因为 Windows Installer 服务使用 SYSTEM 帐户来安装软件。
代码:
:outer for($i=1; $i -le $attempts; $i++) {
$timeout = $null
$proc = Start-Process -filePath $InstallerPath -ArgumentList $InstallCommand -PassThru
$proc | Wait-Process -Timeout $SecondsToWait -ea 0 -ev timeout
If (($timeout) -or ($proc.ExitCode -ne 0)) {
$proc | kill
$error = "`tFailed To …Run Code Online (Sandbox Code Playgroud) 环境:Windows 10,,Java 11.0.10Maven 3.8.1
有一个简单的批处理文件run.bat
mvn clean package
echo After build
Run Code Online (Sandbox Code Playgroud)
call run.bat从命令行执行时,我得到了 Maven 构建的完整输出,但该行After build永远不会打印到控制台。输出的最后一行是Process finished with exit code N(N=0如果构建成功,N=1否则)。
一种可能性是使用start在单独的进程中运行maven,但问题是需要顺序执行:
mvn clean package
command X // depends on the previous command artifact
Run Code Online (Sandbox Code Playgroud)
假设 Maven 在内部调用,我不确定如何实现这一点exit。
有没有办法mvn在同一脚本中与其他命令一起按顺序执行命令?
exit-code ×10
powershell ×3
bash ×2
cmd ×2
java ×2
spring ×2
spring-boot ×2
.net-core ×1
batch-file ×1
boolean ×1
errorlevel ×1
exception ×1
jvm ×1
maven ×1
netcat ×1
python ×1
rhel ×1
subprocess ×1