Hi all, I have something that I'm working on, and with it, I believe I need to fork a process, and retrieve the exit status and PID. A command is exec'ed in the background that essentially is an rsync process
I'm working on a Browser based Rsync backup process: Ajax <-> Perl.cgi -> SETUID C Prog ->Perl(Rsync)
This C program is passed a username and the script to run, and it changes it's effective UID before running the Perl script, so that the rsync command can be run as an ssh login session sudo'ed to the user with environment, and subsequent key pairs, to enable a non-interactive process.
All works so far as I can tell, except for the very last bit - I'm trying to decide on the best approach to launch process, retrieve PID, and Exit Status before writing JSON back to the browser about what happened, and continue to seek read chunks from logfile and also write back to web browser in separate process
When I open pipe as a way to fork and get PID, I get success (on open pipe) even if wrapper process fails. If I exec, I get no PID etc. I know I can use 'pidof' and/or parse 'ps' command etc., but perhaps I'm not seeing a less-convoluted way to approach this?
Any awesome suggestions are welcome
From Rsync Script### run backup command ### my @system_params = ( "-o", "log/nix_backup_wrap", "$var{usern +ame}", "./backup_run.plX" ); my $pid = open my $cmd, "-|", './nix_backup_wrap', @system_pa +rams or warn "$!\n"; my $backup_process_status = $? >> 8; my @backup_status_message; print "BACKUP PROCESS STATUS: $backup_process_status\n"; if ( $backup_process_status == 0 ) { $msglog->write( "Success starting backup process!" ); push @backup_status_message, "Success starting backup +process!"; open 'PID', '>', $PID_FILE or die "Status $var{status} +: Tried to open $PID_FILE $!\n"; print PID "$pid\n"; close PID; } else { $err_msglog->write( "Error starting backup process!" ) +; push @backup_status_message, "Error starting backup pr +ocess!"; } $jsonObject = { msgdata => "BACKUP STATUS MESSAGE: @backup_status_message with P +ID: $pid", pid => $pid, lposition => $var{lposition}, count => $var{count}, segment => $var{segment}, status => $var{status}, backup_process_status => $backup_process_status }; my $jsonEncoded = encode_json $jsonObject; print WB_OUT $cgi->header(-type => "application/json", -charset => + "utf-8"); print WB_OUT $jsonEncoded;
From C Programeval { alarm 5; $msglog->write( "/usr/bin/rsync -avz $var{username}\@$IP:/tmp/$var{ +username}/test /tmp/ | tee -a $TMPLOG" ); exec( "/usr/bin/rsync -avz $var{username}\@$IP:/tmp/$var{username}/ +test /tmp/ | tee -a $TMPLOG") or $err_msglog->write( "STATUS: $var{status}: Couldn't initiate bac +kup $!\n" ); alarm 0; };
/* Set UID to user */ setuid( new_uid ); _user = custom_command( command_whoami ); printf("User after setuid(%d): %s\n", new_uid,_user); /* Now run the actual ".cgi" file */ strcpy( _HOME, "HOME=/home/e-smith/files/users/" ); strcat( _HOME, username ); strcpy( _LOGNAME, "LOGNAME=" ); strcat( _LOGNAME, username ); int ret; char *cmd[] = { script_name, username, (char *)0 }; char *env[] = { _HOME, _LOGNAME, "SHELL=/bin/bash", "SSH_TTY=/dev/pts/ +1", (char *)0 }; ret = execve (script_name, cmd, env); return(0); }
In reply to Open pipe and fork or system command to retrieve PID and exit status - Best approach? by altTesting
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |