Sponsored Content
Top Forums Programming Fork and Execvp Not Executing Bash Commands From C correctly. Post 302969173 by Don Cragun on Saturday 19th of March 2016 03:57:13 AM
Old 03-19-2016
First, note that you can cut a couple of processes out of your pipe line by using:
Code:
lsof 2>&1 | awk '/log/ && /openbox/ && c++ { print $10 }'

instead of:
Code:
lsof 2>&1 | grep log | awk '/openbox/ { print $10 }' | sed '1d'

Then a few comments about your C code:
  1. The fork() function returns 0 in the child (if a child is created successfully) and returns the PID of the child in the parent (if a child is created successfully), or returns -1 in the parent (if a child could not be created). Your code assumes that fork() never fails! Don't make that assumption.
  2. Operands like 2>&1, |, and grep are shell directives; not operands to be passed to the lsof utility.
  3. Instead of executing lsof, you probably need to execute sh with arguments -c and lsof 2>&1 | awk '/log/ && /openbox/ && c++ { print $10 }'.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash: executing commands and reading exit vals

I have a function that returns a bunch of exit codes, say func1, and in my code I'm trying to execute that function in an if statement. This is the closest I could get. f1call=`func1 $arg1 $arg2` if ]; then ... fi When I run the script the function never gets called. What's the right way... (7 Replies)
Discussion started by: eur0dad
7 Replies

2. Programming

after executing execvp()... program hangs up

Hi , I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad: Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up values of... (3 Replies)
Discussion started by: Crab
3 Replies

3. Shell Programming and Scripting

How to get BASH to interpret pipes in a string correctly?

In a BASH library I'm creating, I have two functions that look like: function check_process { PIDFILE=$1 if ; then PID=`cat $PIDFILE` if && ; then return 1 fi; fi; return 0 } function fork_process { CMD=$1 PIDFILE=$2 ... (2 Replies)
Discussion started by: neked
2 Replies

4. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

5. Red Hat

How commands are executing ?

Hi Folks, I have a small doubt, the binary commands under /bin and /sbin as well as other path binary files, if you peek deep into that, you can find the difference in the way of normal perl programming and some commands will be like binary files. how are the commands executing like the... (3 Replies)
Discussion started by: gsiva
3 Replies

6. Shell Programming and Scripting

Executing many commands at once

Hi, I want to run these two commands one after the other. awk 'BEGIN {OFS="\t"} {print $2}' sort -u rather than typing awk 'BEGIN {OFS="\t"} {print $2}' file1 > file2, then sort -u file2 > file3. Is it possible to run both commands on file1 then get output file3? Its kinda hard for... (5 Replies)
Discussion started by: kylle345
5 Replies

7. Programming

If I malloc my parameters, then fork and execvp, where do I free them?

Hi, I do the following: i) malloc some parameter structures ii) fork iii) in the child, I call execvp using these parameters obviously, here I can never free() the structures as execvp should not return iiii) I am currently doing waitpid in parent then freeing, but this does not seem to work... (1 Reply)
Discussion started by: misha680
1 Replies

8. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

9. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

10. Shell Programming and Scripting

Executing bash file with sudo for the second time, leads to permission denied, for some commands

I have a script that checks if the script has been ran with sudo. If the script is not ran as sudo, the current script is being executed with exec sudo bash. You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies
PTHREAD_ATFORK(3)					     Library Functions Manual						 PTHREAD_ATFORK(3)

NAME
pthread_atfork - register handlers to be called at fork(2) time SYNOPSIS
#include <pthread.h> int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); DESCRIPTION
pthread_atfork registers handler functions to be called just before and just after a new process is created with fork(2). The prepare han- dler will be called from the parent process, just before the new process is created. The parent handler will be called from the parent process, just before fork(2) returns. The child handler will be called from the child process, just before fork(2) returns. One or several of the three handlers prepare, parent and child can be given as NULL, meaning that no handler needs to be called at the cor- responding point. pthread_atfork can be called several times to install several sets of handlers. At fork(2) time, the prepare handlers are called in LIFO order (last added with pthread_atfork, first called before fork), while the parent and child handlers are called in FIFO order (first added, first called). To understand the purpose of pthread_atfork, recall that fork(2) duplicates the whole memory space, including mutexes in their current locking state, but only the calling thread: other threads are not running in the child process. The mutexes are not usable after the fork and must be initialized with pthread_mutex_init in the child process. This is a limitation of the current implementation and might or might not be present in future versions. RETURN VALUE
pthread_atfork returns 0 on success and a non-zero error code on error. ERRORS
ENOMEM insufficient memory available to register the handlers. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
fork(2), pthread_mutex_lock(3), pthread_mutex_unlock(3). LinuxThreads PTHREAD_ATFORK(3)
All times are GMT -4. The time now is 08:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy