Running /scripts/pkgacct using system(), exec() or backticks - perl

I have a small perl script which runs the /scripts/pkgacct command in cPanel using system(). The code looks like so;
print "\n/scripts/pkgacct --skiphomedir --nocompress $acc_name /my_backup\n\n";
system("/scripts/pkgacct --skiphomedir --nocompress $acc_name /my_backup");
my $bk_path = "/my_backup/cpmove-$acc_name.tar";
system("tar -xvf $bk_path -C /my_backup/");
When I run the script, only cPanel's default roundcube and horde databases are backed up. When I replace system() with exec"", the script runs as expected but terminates as soon as exec is executed, i.e the subsequent statements in the perl script aren't executed. Using backticks shows the same behaviour as system() - i.e doesn't backup all the databases.
Could someone tell me what mistake I am making?
Alternately, how can I get the remaining statements to execute after the exec command?

Try using system like so:
system('/scripts/pkgacct', '--skiphomedir', '--nocompress', $acc_name, '/my_backup');
I've found that system works best when you break up the command and parameters like it expects.

Try using IPC::Run (https://metacpan.org/pod/IPC::Run). Your code would look something like:
use IPC::Run qw(run);
print "\n/scripts/pkgacct --skiphomedir --nocompress $acc_name /my_backup\n\n";
run ['/scripts/pkgacct', '--skiphomedir', '--nocompress', $acc_name];
my $bk_path = "/my_backup/cpmove-$acc_name.tar";
run ['tar','-xvf',$bk_path,'-C','/my_backup/'];

Related

Run perl script from tcl

I have a perl script that I manually run it from shell:
myScript.pl -from_session=Session1234 -to_session=Session6789 -port=104:3445
I try to run it from tcl, in this way, but it doesn't work....
set script_path "myScript.pl";
set sessionA "Session1234";
set sessionB "Session6789";
set p "104:3445";
exec perl $script_path -from_session=$sessionA -to_session=$sessionB -port=$p
Update:
I found that the perl script is done, but the tcl script is not continue after the execution of the perl script.
TCL doesn't need semicolon at the end. Once removed, I tried to execute exec command on one of my PERL script in tclsh and it works fine.
Therefore my suggestion is to try executing the program in tclsh to see weather some error is returned and most importantly to give the full absolute path to the script. All in all, try
set script_path "/absolute/path/to/myScript.pl"
set sessionA "Session1234"
set sessionB "Session6789"
set p "104:3445"
exec perl $script_path -from_session=$sessionA -to_session=$sessionB -port=$p

How to use debugger on perl script executed by "exec"

I trying to figure out how a Perl script which is doing test status reporting, is working. The script executes another piece of perl script via exec. I am able single step through code in first script but when it hits exec, the script executed by exec runs till completion. Is there a way by which I will be able single step and look at variables in the script executed by exec?
Add below to the script which is being called with exec
#!/usr/bin/perl -d

Would like to take a Perl variable and pass it to bash

I am trying to enter storage commands like symmask in Unix. I would like to build a script that would take a variable from Perl like standard input and then use that variable in a bash shell. I have been trying to do both in Perl but I can't run the storage command in Perl script. Unless I am just missing it.
You can run all external programs and commands from Perl with system, exec and the backtick-operator (`` and qx()).
Please refer to:
http://perldoc.perl.org/functions/system.html
http://perldoc.perl.org/perlop.html#%60STRING%60
http://perldoc.perl.org/functions/exec.html
If you want to, say, copy stuff to another server, you can use the backticks like this:
my $file = 'foo.csv';
`scp foo.csv someone#otherserver:dir/foo.csv`;

How to invoke Unix "script" command in Perl?

I'm sure this is an easy fix, but I need to use "script" (and not collect standard in/out/error) for my project. I'm somewhat new to Perl, so please bear with me.
I have a Perl script that works fine. When I run it I generally type script > filename before I run Perl.
$script > file.log
bash-3.2$ perl foobar.pl
This runs fine, and when I'm done I type exit or control D to stop the script and save the file. All I'd like to do is incorporate the script command in Perl and then automatically capture the file when the program stops running (12-16 hours). The problem I have is that is I call in system("script > file.log"); and them call system("perl foobar.pl"); it hangs at the bash-3.2$ prompt. The only way to get Perl to work is control D or exit, stopping the script function.
Anyone have any idea how to fix this? While it's easy to start with script before invoking Perl, if I'm a mole and forget, I have to rerun the program (which takes a long time).
Have you considered using system("script -c 'perl foobar.pl' file.log")?

Change shell within Perl script

I am looking for a nice way to get the following done:
So I have a script that I need to run in Python in Unix by calling from a Perl script that was, in turn, called from my Excel VBA macro in Windows using Plink. The Python script, due to dependency issues, has to run in either csh or bash, and I will need to use export/setenv to add a few libraries before running the script. However by default, perl runs in sh shell and as such, there is no way I can add in all the dependencies and have the Python script to run.
So, I am just wondering if there is EITHER: 1. a way for me to add dependencies to sh shell in the perl script, OR 2. force my perl script to run in csh (preferred, since for some reason .bashrc for the account runs into permission issues).
Thanks a lot!
How about "3. Set the appropriate environment variable in the Perl or Python scripts"?
$ENV{'PATH'} = ...
...
os.environ['PATH'] = os.pathsep.join(newpaths + os.environ['PATH'].split(os.pathsep))
(dunno how to get the path separator in Perl, sorz)
To force the shell to csh, try the following in Perl :
`/bin/csh -c "command_name"`;
Edit:
You can use ENV variable, like this. Try that :
$s = `/bin/bash -c 'VAR_FOO=753; echo \$VAR_FOO'`;
print $s;
I ended up just change the .cshrc script, apparently the addition to PATH, for some reason, did not work for me. After that, everything runs smoothly by putting all into one line
so basically it looks something like this
/path/to/.cshrc && /python/path/to/python
Hope that helps!