Showing posts with label process. Show all posts
Showing posts with label process. Show all posts

Tuesday, December 17, 2013

[BASH SCRIPTING] Get exit status on the piped process

#!/bin/bash

function logging() {
  while read line; do
    echo "{$(date +'%Y-%m-%d %H:%M:%S')} $line"
  done
}

function stdintoexitstatus() {
  read exitstatus
  return $exitstatus
}

cmds=(
    "cal"
    "cal lkjhsd"
)
for i in $(seq 0 $((${#cmds[@]}-1)))
do
    echo "--------------------------------------------------------"
    echo "[COMMAND] ${cmds[$i]}"
    echo "--------------------------------------------------------"
   ((((eval ${cmds[$i]} 2>&1; echo $? >&3) | logging >&4) 3>&1) | stdintoexitstatus) 4>&1
    echo "Exit Status[$?]"
    echo
done

COMMAND:  eval ${cmds[$i]} 2>&1
The output and the error messages of the eval are redirected to stdout

COMMAND:   echo $? >&3
The status of the eval is stored in the file descriptor 3

COMMAND:   | logging >&4
The (output/error)stdout from eval is piped to logging function where we do logging
and the output of the logging function is stored in file descriptor 4

COMMAND:   3>&1 | stdintoexitstatus
Get the status of eval from file descriptor 3 and update $?

COMMAND:   4>&1
Get the output of the logging function from file descriptor 4 and redirect them to stdout

Reference: http://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another

Monday, October 25, 2010

Signal handler is not your regular function

Signal handler is a function which will be called when the program receives a signal. Most of the signals have their own default handlers and for few the parent program should handle/ignore this signal explicitly. In case of SIGCHLD, if the parent process should define the handler explicitly and wait for the child to exit (we can use SIGIGN too and no need to wait),otherwise the child process will become zombie.

Signal handler can not do everything what any normal function can do. It can call the functions and system calls which are async-safe.
This is the list of async-safe fuctions and system calls,

_Exit(), _exit(), abort(), accept(), access(), aio_error(), aio_return(), aio_suspend(), alarm(), bind(), cfgetispeed(), cfgetospeed(), cfsetispeed(), cfsetospeed(), chdir(), chmod(), chown(), clock_gettime(), close(), connect(), creat(), dup(), dup2(), execle(), execve(), fchmod(), fchown(), fcntl(), fdatasync(), fork(), fpathconf(), fstat(), fsync(), ftruncate(), getegid(), geteuid(), getgid(), getgroups(), getpeername(), getpgrp(), getpid(), getppid(), getsockname(), getsockopt(), getuid(), kill(), link(), listen(), lseek(), lstat(), mkdir(), mkfifo(), open(), pathconf(), pause(), pipe(), poll(), posix_trace_event(), pselect(), raise(), read(), readlink(), recv(), recvfrom(), recvmsg(), rename(), rmdir(), select(), sem_post(), send(), sendmsg(), sendto(), setgid(), setpgid(), setsid(), setsockopt(), setuid(), shutdown(), sigaction(), sigaddset(), sigdelset(), sigemptyset(), sigfillset(), sigismember(), sleep(), signal(), sigpause(), sigpending(), sigprocmask(), sigqueue(), sigset(), sigsuspend(), sockatmark(), socket(), socketpair(), stat(), symlink(), sysconf(), tcdrain(), tcflow(), tcflush(), tcgetattr(), tcgetpgrp(), tcsendbreak(), tcsetattr(), tcsetpgrp(), time(), timer_getoverrun(), timer_gettime(), timer_settime(), times(), umask(), uname(), unlink(), utime(), wait(), waitpid(), and write().

Ref : http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html#signals