simultaneously need to exit current perl script while opening the pdf file - perl

I have a script to open the pdf file (Not in Adobe Acrobat application) instead Sumatra application (Pdf viewer).
system("start Sumatra C:/Users/test/Desktop/19June.pdf");
exit;
The problem is once we need to close the pdf file then only the script exited. But the requirement is the tool will be exit and not considered the opened pdf file.

Use exec instead of system. exec replaces the current process with the one given, while system will start a new process and wait for it to exit.

Related

Acquire lock for /etc/passwd file in Bourne shell script

I have an embedded system where user management in /etc/passwd file is done usually automatically with a Bourne shell script. However, it might happen that sometimes /etc/passwd file is edited with a text editor by root user or by passwd utility. Is there a way to program Bourne shell script in a way that it locks the /etc/passwd file during its execution so that other programs are not able to edit the file at the time? Also, this script should detect if /etc/passwd file is not opened by other processes. I could use following solution from Wooledge wiki:
# locking example -- CORRECT
# Bourne
lockdir=/tmp/myscript.lock
if mkdir "$lockdir"
then # directory did not exist, but was created successfully
echo >&2 "successfully acquired lock: $lockdir"
# continue script
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
exit 0
fi
However, this ensures only that two instances of this script are not running simultaneously. I also have a BusyBox lock available which behaves similarly to flock, but again, as far as I can tell, I can't protect other processes editing /etc/passwd file.
The vipw command may provide this for you and you can customize the editor using the EDITOR environment name.
See man vipw for details.
Is there a way to program Bourne shell script in a way that it locks the /etc/passwd file during its execution so that other programs are not able to edit the file at the time?
That is called mandatory file locking and the answer is probably no. In Linux that requires the mand option when the file system is mounted. I would guess that's a nonstarter in your environment, but if it's an option (so to speak) have a look at your favorite resource for how to proceed from there.
It's not shell-script functionality you need. For one process to prevent another from opening a file requires kernel support. Unix programs traditionally use advisory locks, or cooperate some other way. vipw(8) is an example of how that's done.

Perl Strawberry: How to run a perl script by clicking

I'm working on Windows 7 and I have installed Strawberry. I would like to run a perl skript (test.pl):
open OUTPUT, ">test.txt";
print OUTPUT "This is a test\n";
by just clicking on the file or redirect with left mouse click to Perl-program (open with/perl.exe). When I do this a console opens for less than a second and disappears but the file test.txt is not created. If I go to the MS command and enter
> C:\myplace>perl test.pl
it works. I never had this experience before (WinXP, other Windows 7 PC with ActivePerl and Windows 8 with strawberry). I would be very happy if somebody could give me a hint how to solve this problem.
There are two problems here:
Creating the file where you want it. When double-clicking a perl script to launch it, it is not executed in the context of the folder you have opened in Explorer. If you want to specify an explicit context, do the following near the top of your script:
use FindBin; # This is a module that finds the location of your script
BEGIN { chdir $FindBin::Bin } # set context to that directory.
When you then create a new file without an aboslute path, the path is considered relative to that directory.
You do not have the problem when running the script from the command line, because you have specified the correct path. But if you run it from C:\ like
C:\> perl myplace/test.pl
then you have created the file in C\test.txt. The FindBin solution fixes this.
When running a script by double-clicking it, the command line exits before you can inspect the output. This “problem” is shared by all programming languages on Windows. You can force the window to stay open by waiting for some input. You can either do
system("PAUSE"); # not portable to non-Windows!
or
warn "Press enter to exit...\n";
<STDIN>; # Read a line from the command line and discard it.
# Feels akward when launching from the command line
to wait until an Enter ⏎ is pressed .
The other solution is to always use the command line for your scripts, which is what I'd actually suggest.
Check what is your script executing folder, as it might differ from C:\myplace
use Cwd;
print getcwd();
sleep 7;

prints in an another window in cmd and close quickley

I am trying to run a Perl script from command prompt.
The script contains one line:
print "Hello World!\n"
I type in the cmd: Perl hello.pl
The line is printed in a new window and quickly is closed.
It's all happening in the cmd! Does anyone had this kind of problem?
I know Perl is working because I tried to run a script that creates an excel file and it worked.
The only problem is, that it doesn't print in the same window as it is supposed to do, but opens a new window, prints there and closes it. (I tried to do a while loop in the end and it didn't help).
I was able to solve this.
In windows there is an option called "Open command prompt as Administrator". A new window does not open up in that case.
The cmd window closes as soon as the command that it runs has exited. You can either
… start a cmd.exe of your own, and launch your script via
> perl C:\path\to\script.pl
instead of double-clicking the perl file (or whatever you are doing to start it). This should not start a new window.
… or you could have the script wait until you have read the message. Just wait for user input of some sort before exiting, e.g. like
<>; # read and discard a line to exit
at the bottom of your script.
You can also use the pause program for this, which you can execute like system('pause').

Perl Win32 - Geting file handle provided file and removing it

I have a file (ex. C:\temp\afile.txt) in which a Windows service has an open file handle on. After stopping the process, the file handle remains open. I would like to be able to find and delete this handle simply provided the file name and path with a Perl script. Is this possible? Thank you for your time.
It is possible to locate which process hold a file handle open and to reach into the process and kill the handle because MS's Process Explorer can do just this. How? I don't know.
You should probably use MoveFileEx(file_name, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) instead. This causes the file to be deleted the next time the system is rebooted.
Win32API::File provides a Perl interface to that system call.

How do I get the command prompt do dissapear after starting a program via a batch script?

I have a windows batch file which I run to start a java application. The problem is that I don't want the command prompt output to be visible after the app starts. And not only that,... I don't event want to see it minimised. I don't want it at all. Any ideas?
Cheers!!
Use
start/b javaw.exe ...
If your program is not a console application, you can use START.EXE in your batch file to actually launch the real app. The initial console used to launch the batch file will be closed when the batch file ends.
You could actually probably launch the .bat file with start /b too in order to avoid all console windows.