Showing posts with label ShellScript. Show all posts
Showing posts with label ShellScript. Show all posts

Thursday, August 22, 2013

redirect stdout and stderr to a file

program [arguments...] 2>&1 | tee -a outfile &

2>&1: directs stderr to stdout

tee receives the content from stdout and write them to a file. -a means append
see man tee for more info.

E.g.,

I want to append the openvz migration log message to a file. This shell script migrates 100 openvz containers in parallel, and wait all of them to finish. It outputs the time at the end.

#!/bin/sh

echo "*********************************************************" | tee -a migrationResult.txt


date | tee -a migrationResult.txt


for i in {1..100}

do
        vzmigrate --live -t  sr1s1 $i 2>&1 | tee -a migrationResult.txt &
done
wait // wait all of the processes to finish
date | tee -a migrationResult.txt

Wednesday, March 30, 2011

How to run 100 apps

1. Using shell script, and run all the apps by background

#!/bin/bash
i=1
while [ $i -le 5 ]
do
    ./while1&
    (( i++ ))
done


2. Using fork and exec
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TIMES 100
#define PROGRAM "./while1"

int main()
{
    int pid;
    int i;
    for(i = 0; i< TIMES; i++)
    {
    pid = fork();
    if(pid == -1)
    {
        fprintf(stderr, "fork() failed\n");
        exit(1);
    }
    else if(pid == 0)
    {
        // child process
        execv(PROGRAM, NULL);
    }
    else
    {
        // parent process
        // wait(0); //comment out this line, you only can fork 1 program
    }
    }
//    wait(0); // comment out this line if you want to this process to wait 100 while1 program
    return 1;
}

Thursday, March 24, 2011

shell script in Windows

create a bat file named run.bat
edit the run.bat
you could put command which windows could understand
E.g.
dir
cmd
measureSuspendTime.ext
psshutdown -d -t 0

Tuesday, September 21, 2010

Shell Script while loop

Shell script is space sensetive,
There are space between "[" and "$i"
you have to use double parentheses.


While loop:

#!/bin/bash
i=1
while [ $i -le 100 ] # loop 100 times
do
./readCMOSflag
i=$(( $i + 1 ))
# or you could use (( i++ ))
done