INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

A simple code runs at cmdline, but not in cron job

A simple code runs at cmdline, but not in cron job

A simple code runs at cmdline, but not in cron job

(OP)
A simple code just like this:

CODE

my $cmd = "gwsh root\@$ip \"uname -r\"";
my $ret = `$cmd`; 

When I ran the code at cmd prompt, it's fine.

But when it's run in a cron job, $cmd returns nothing.

What did I miss here?

Many thanks.

RE: A simple code runs at cmdline, but not in cron job

(OP)
In addition, while the code is executed at cmd prompt, following three variables are the same as my own login ID:
$ENV{ LOGNAME }, getpwuid( $< ), & $ENV{ USER }.

However, while it's executed by the cron job, then $ENV{ USER } is undefined. But both $ENV{ LOGNAME } and getpwuid( $< ) are still the same as my own login ID.

Thanks.

RE: A simple code runs at cmdline, but not in cron job

(OP)
I got the solution. I need to set up two ENVs to make cron job run. Basically calling setEnv() before invoke gwsh. My sample code is listed below:

CODE

sub setEnv {
	my $sshPid = getSshPid(user=>$ENV{LOGNAME}, psName=>'ssh-agent');
	my $sshAuthSock = getSshAuthSock(sshPid=>$sshPid);

	my %newEnvs = (
		'SSH_AGENT_PID', $sshPid,
		'SSH_AUTH_SOCK', $sshAuthSock,
	);

	foreach my $k (keys(%newEnvs)) {
		if(!exists($ENV{$k}) || !$ENV{$k}) {
			$ENV{$k} = $newEnvs{$k};
		}
	}
}

sub getSshAuthSock {
	my %args = (@_);

	my $FPID = $args{sshPid} - 1;

	my $cmd = "find /tmp -path \"*ssh*\" -type s -iname \"agent.$FPID\"";
	my $sshAuthSock = `$cmd`;
	chomp($sshAuthSock);

	return $sshAuthSock;
}

sub getSshPid {
	my %args = (@_);

	my $cmd = "ps -ef | grep $args{user} | grep $args{psName} | grep -v grep";
	my $ret = `$cmd`;
	my $pid = (split(/\s+/, $ret))[1];

	return $pid;
} 

Question:
I used shell cmd in my perl code. Could someone show me how to implement this in PURE perl code?

Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close