upstart System.in.read() crashes the process? - upstart

My java application is only this:
System.out.println("waiting for input...");
PrintWriter writer = new PrintWriter("/home/pc/test-java.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
System.in.read();
The file is created just fine, then the process seemingly dies without warning. I could not find anything in the docs that would explain this behavior. Am I not allowed to wait for input at all? How would upstart know that I'm trying to read input and prevent it? Why does it care? I'm only upstart script testing here.
Here is the full script:
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 2 5
script
exec java -jar /home/pc/test-0.0.1-SNAPSHOT.jar
end script

Some relevant snippets from the Upstart documentation for the console stanza:
For all versions of Upstart prior to v1.4, the default value for console was console none. As of Upstart 1.4, the default value is console log.
6.5.1 console log
Connects standard input to /dev/null.
6.5.2 console none
Connects the job's standard input, standard output and standard error file descriptors to /dev/null.
6.5.3 console output
Connects the job's standard input, standard output and standard error file descriptors to the console device.
So, in all versions of Upstart, stdin is mapped to /dev/null by default. Thus, System.in.read() immediately reads end-of-file (EOF) and returns.
To get the behaviour you seem to want, add either console output or console owner to your Upstart job definition:
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 2 5
console output
# OR
#console owner
script
exec java -jar /home/pc/test-0.0.1-SNAPSHOT.jar
end script

Related

Silent bat file execute powershell command

We have an application server running as a service, when some configuration is loaded it starts a bat script which has to run the powershell command Stop-ClusterGroup DRMSERVICES and then start it again.
The bat file works flawless when I manually execute it by dobbelt clicking. But when the service is running the bat, it does not finish, or execute the powershell command.
Bat file looks as follows
#echo off
powershell -command Stop-ClusterGroup DRMSERVICES
powershell -command Start-ClusterGroup DRMSERVICES
The service runs the bat file in silent mode, as a main difference.
I have tried with various switches including the -ExecutionPolicy Unrestricted and START /wait etc
Creating a seperate ps1 file and have the bat execute this instead.
All with the same output:
Manually executing the bat works
When the service executes the bat, it does not work.
I know the bat file is executed by the service, as inserting NET STOP servicename is working correct.
In the powershell event viewer I can also see event of the powershell commands take place.
The difference between manually executing and have the service execute the command in the event viewer, is event id 800 which states info about 'execution pipe' this is not present when the service is executing the bat.
The service does not wait for the powershell, and thus it does not have time to stop the cluster before exiting.
I'm lost whether this is a permission issue, syntax error or whatever.
Hopefully somebody can help
UPDATE:
I have tried with all proposed solutions, all with same result, the bat file works when double clicked, but the service does not execute the powershell command. Pure cmd is executed, as I can pipe to a txt file. I even got to a point when trying runas that the output log text wrote "insert administrator password"
I even managed to have our software guy change our software to call a powershell directly instead of a bat, same result. Powershell won't execute the command, this tells me it probably is permission, but everything have been set to log in as admin and run as admin for the sake of success, but still nothing.
I solved the problem.
Because the service is a 32bit process, it will execute a 32bit powershell.
FailoverClusters module only exists as a 64bit module.
By using %SystemRoot%\sysnative\WindowsPowershell\v1.0\powershell.exe
The service is able to open a 64bit session, and thus use the failover cluster module.
As a side note, the sysnative folder is only visible from a 32bit session, therefore it cannot be found via browsing in a 64bit os.
I think i have dealt with this kind of issue before, after the,
powershell -command Stop-ClusterGroup DRMSERVICES
you need to have cmd wait for a certain number of seconds, and then test if the DRMSERVICES is now stopped, if it is stopped then to start the DRMSERVICES again. This way cmd will keep waiting, and then check if the service has stopped.
After a certain number of tries, maybe have a way to stop checking and exit the script, for example it is trying to stop the service, and has run into a problem.
There is a timeout command in cmd

Perl script file run manually but not in crontab

I have a perlscript file was running fine in crontab but suddenly it stopped running without any modification.
cd /home/user/public_html/crons && ./script.pl 2>&1 >/dev/null
The top of the script file is #!/usr/bin/perl -X
The output expect from this script is changes in database
I have another script file with the same modification and still works fine
When I run the file in the browser it works fine and execute all lines without any problem
I tried full path /usr/bin/perl but it didn't work
I tried Perl at the beginning but it didn't work
I run the command from SSH using putty but nothing happened
I checked log file /var/log/cron but no errors at all
I created temporary log file cd /home/user/public_html/crons/script.pl> /tmp/temp.log 2>&1 to see the errors but the log is empty
Here is the solution:-
I found the issue, There is was a stuck process for the same cron file , so i killed this process and its fixed
You can find your file process like this
ps aux | grep 'your cron file here'
This is a really common antipattern people seem to tend toward with cron.
Cron sends you an email with the output of your script, if it generates any output. People often redirect output to /dev/null to prevent cron from sending the email. This is bad because now the output of your script is lost entirely. Even if the script has some built-in logging, it might generate errors before it gets the log file opened and those are lost. It also might crash in a way that doesn't get written to the logging mechanism.
At a bare minimum, you should just remove 2>&1 >/dev/null to start receiving the email. (and also, test your mail setup using a temporary cron job like 1 * * * * echo "Test" )
The next better solution is to change it to >> /var/log/myscript/current.log and then also set up something to rotate the log files (like logrotate) and also make sure to create that directory with permissions that the script user is allowed to write to it. By only redirecting STDOUT of the script, any errors or warnings it writes to STDERR cause you to get an email, and if there are no errors/warnings the output goes to the log file and no email gets sent.
Neither of those changes solve the root problem though, which is that when cron runs your script it does so with a different environment than you have on the command line. What you really want is a way to run the script with a consistent environment, and log it. The "ultimate solution" is to define your task in some kind of service manager, and then use cron to occasionally start it. For instance, you could use systemd and define a service that doesn't restart, then use systemctl start my_custom.service in your cron job. Now you can test independent of cron, and your tests will have the same exact environment, and be logged by the service manager. As extra bonuses, you are protected from accidentally running your script twice at once, and you get a clean way to stop a running cron job without the danger of stale pid files.
I don't particularly advocate systemd myself, but thankfully there are lots of alternatives:
Runit : http://smarden.org/runit/runsvdir.8.html
S6 : https://skarnet.org/software/s6/
Perp : http://b0llix.net/perp/site.cgi?page=perpd.8
(but installing and configuring a service manager is a bigger task than just using systemd if your distro is based on systemd) Each of these allows you to define a service that doesn't restart. Then you use a shell command to issue a "run once" directive to the supervisor, which runs the task as a child. Now you can easily launch the jobs yourself and see all the errors in the log, and then add that command to the crontab and know that it will run identically when cron starts it.
Back to your original problem, once you get some logging you are likely to discover it is a permission problem or a upgraded module in the system perl.

how to redirect output of application in command window, which started with affinity?

the following command starts the C++ application in new command window with process affinity set to 0xF.
start /affinity F test.ext arg1 arg2
But the above command opens a new cmd window and closes immediately when test.exe ends. I tried the following to get output but it doesn't do anything.
start /affinity F test.ext arg1 arg2 ^> out.txt
I appreciate if you know how to do this on powershell.
Thanks
About Redirection
The PowerShell redirection operators are as follows, where n
represents the stream number. The Success stream ( 1 ) is the default
if no stream is specified.
Operator Description
Syntax
> Send specified stream to a file. n>
>> Append specified stream to a file. n>>
>&1 Redirects the specified stream to the Success stream. n>&1
# Examples
# Example 1: Redirect errors and output to a file
dir 'C:\', 'fakepath' 2>&1 > .\dir.log
This example runs dir on one item that will succeed, and one that will
error.
It uses 2>&1 to redirect the Error stream to the Success stream, and >
to send the resultant Success stream to a file called dir.log
# Example 2: Send all Success stream data to a file
.\script.ps1 > script.log
This command sends all Success stream data to a file called script.log
Also, a possible duplicate of the following
How to pipe all output of .exe execution in Powershell?
How to redirect the output of a PowerShell to a file during its execution
See also
Running external commands, can or will require special
consideration.
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Using Windows PowerShell to run old command line tools (and their
weirdest parameters)
Execution of external commands in PowerShell done right
Part 1
Part 2
Part 3
Quoting specifics

Execute jar and display text in jenkins console log

I have abc.jar file to deploy and run in remote machine.
I have transferred the file using jenkins, now what I have done is, call a a.bat batch file on remote machine using psexec in Execute Windows Batch Command.
a.bat executes the abc.jar
When the jar begins execution, the command prompt texts are stored in a file.
using java -jar abc.jar >> a.log 2>&1
Now what I want is to display the a.log contents in the jenkins console when the jar file is being executed
(the file is continuously being written and I want to show it in jenkins console as it is being written)
I have tried to do it using parallel processing by calling start twice, one for calling batch file, another using type for displaying.
But when I use start I get Process leaked file descriptor .
Is there any other way I can achieve this. Be it calling powershell or scheduled task in jenkins.
You need to look for tee equivalents in windows , there are few like GNU utilities for Win32, however if you have cygwin you can still use tee which will easy the prcoess.
Now the wuestion arises how to run my jar file on cygwin from jenkins ?
you can still use execute windows[batch] shell. and add cygwin installation path to the PATH variable and start using linux command like a BOSS.
or you can use powershell tee in built command from batch.

How to get Current Stdout from process

I am trying to write a PowerShell script that will run a console app which is a .exe file, poll the stdout from the .exe, and depending on what get's printed to the console, will invoke a key-stroke or exit (Ctrl+C) the application. I want to be able to read the console output while the process is still running. So I don't want to wait until the process is complete.
Initially, I tried to accomplish this by creating a System.Diagnostics.Process object and calling .Start() on that process and then getting the .StandardOutput from that object. But that looks like it waits until the process is complete as I have tested the ping command.