Sponsored Content
Homework and Emergencies Homework & Coursework Questions script to write PIDs to a file Post 302572266 by navlelo on Wednesday 9th of November 2011 12:33:39 PM
Old 11-09-2011
script to write PIDs to a file

script to write PIDs to a file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

write a script that writes the PID of 2 different processes to a file every other time. as in pid a , pid b, pid a, pid b etc. then write a script that calculates the average runtime of 100 run throughs. (supposed to write a similar program in c that writes the PIDs aswell, to compare runtimes of the 2)
2. Relevant commands, code, scripts, algorithms:

bash
/usr/bin/time - built in gnu time thingy
octave - calculator for the time script



3. The attempts at a solution (include all code and scripts):
Code:
 
#!/bin/bash
function child{
        for X in {0..6}
do
                echo "$$"
                sleep 1
        done
        return 0;
}
function parent{
        for I in {0..6}
 do
                echo "$$"
                sleep 1
        done
        return 0;
}
#Start child function
child &
 
# Start start parent function
parent

this was supposed to get me the 2 PIDs, but not even that seems to work. will redirect them once I get them to return correctly
getting this error when I try to run it as executable
Code:
./pidbash.sh: line 8: return: can only `return' from a function or sourced script
./pidbash.sh: line 9: syntax error near unexpected token `}'
./pidbash.sh: line 9: `}'


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Norwegian University of Science and Technology NTNU, Trondheim, Norway, Svein Erik Bratsberg, TDT4186 Operativsystemer, TDT4186 Operativsystemer
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to AutoKill PIDs of LOGED in Active User

:confused: Hi i am trying to make a script through which i can get the total number of User and their PID after that kill all Users except the one by which i am currently logged in. (i m using SCO UNIX 5.06) # id of current usre < total number of current user USR=`who -u |wc -l` # id of... (2 Replies)
Discussion started by: aafflatoon
2 Replies

2. Shell Programming and Scripting

Shell Script for PIDs

I am trying to write a Shell script wherein the shell needs to read a list of PID in the File $stat/bin/Process and compare it to the PID of the processes running on a server. Also the script should return KO(not OK) with corresponding label :- a) When an environmental variable not... (2 Replies)
Discussion started by: marconi
2 Replies

3. Shell Programming and Scripting

Can we grep a list of all running PIDs in a file !!??

Hi, In the following part of a script, I am grepping the list of all running PIDs in the File as in line 3 :- $pid_count=`grep -c "^${pid_process}$" $CRI_PUSH_BIN_HOME/bin/ PushProcessId` If I cannot grep this way, then how can I do so. 1 pid_process=`ps -ef -o pid,args |... (1 Reply)
Discussion started by: marconi
1 Replies

4. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

5. Shell Programming and Scripting

need shell script to get last 10 char from a file name and write in to a new file

i have no idea abount shell script but i need need shell script to get last 10 char from a file name and write in to a new file. consider u hav 5 files in a particular dir i script should get last 10 char of each file n write the 10 char in separate files (2 Replies)
Discussion started by: raj0390
2 Replies

6. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

7. Shell Programming and Scripting

Write script to call a file

Hello Experts, I have been trying to work on to call a file in scripts. @ECHO OFF call %MY_HOME%\bin\setvars.bat call %MY_HOME%\db\bin\start_db.bat. This is a batch file and i want to write same functionality in shell script. How should I achieve it? How should I set variables in a... (1 Reply)
Discussion started by: rajuchacha007
1 Replies

8. Shell Programming and Scripting

Script to write result to a file

Hello, How can I run this script every 1 hour and save its result to result.txt ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' Regards Shaan (5 Replies)
Discussion started by: Shaan_Shaan
5 Replies

9. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

10. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies
Proc::Daemon(3pm)					User Contributed Perl Documentation					 Proc::Daemon(3pm)

NAME
Proc::Daemon - Run Perl program(s) as a daemon process. SYNOPSIS
use Proc::Daemon; $daemon = Proc::Daemon->new( work_dir => '/my/daemon/directory', ..... ); $Kid_1_PID = $daemon->Init; unless ( $Kid_1_PID ) { # code executed only by the child ... } $Kid_2_PID = $daemon->Init( { work_dir => '/other/daemon/directory', exec_command => 'perl /home/my_script.pl', } ); $pid = $daemon->Status( ... ); $stopped = $daemon->Kill_Daemon( ... ); DESCRIPTION
This module can be used by a Perl program to initialize itself as a daemon or to execute ("exec") a system command as daemon. You can also check the status of the daemon (alive or dead) and you can kill the daemon. A daemon is a process that runs in the background with no controlling terminal. Generally servers (like FTP, HTTP and SIP servers) run as daemon processes. Do not make the mistake to think that a daemon is a server. ;-) Proc::Daemon does the following: 1. The script forks a child. 2. The child changes the current working directory to the value of 'work_dir'. 3. The child clears the file creation mask. 4. The child becomes a session leader, which detaches the program from the controlling terminal. 5. The child forks another child (the final daemon process). This prevents the potential of acquiring a controlling terminal at all and detaches the daemon completely from the first parent. 6. The second child closes all open file descriptors (unless you define "dont_close_fh" and/or "dont_close_fd"). 7. The second child opens STDIN, STDOUT and STDERR to the location defined in the constructor ("new"). 8. The second child returns to the calling script, or the program defined in 'exec_command' is executed and the second child never returns. 9. The first child transfers the PID of the second child (daemon) to the parent. Additionally the PID of the daemon process can be written into a file if 'pid_file' is defined. Then the first child exits. 10. If the parent script is looking for a return value, then the PID(s) of the child/ren will be returned. Otherwise the parent will exit. NOTE: Because of the second fork the daemon will not be a session-leader and therefore Signals will not be send to other members of his process group. If you need the functionality of a session-leader you may want to call POSIX::setsid() manually at your daemon. INFO: Since "fork" is not performed the same way on Windows systems as on Linux, this module does not work with Windows. Patches appreciated! CONSTRUCTOR
new ( %ARGS ) The constructor creates a new Proc::Daemon object based on the hash %ARGS. The following keys from %ARGS are used: work_dir Defines the path to the working directory of your daemon. Defaults to "/". setuid Sets the real user identifier ($<) and the effective user identifier ($>) for the daemon process using "POSIX::setuid( ... )", in case you want to run your daemon under a different user from the parent. Obviously the first user must have the rights to switch to the new user otherwise it will stay the same. It is helpful to define the argument "setuid" if you start your script at boot time by init with the superuser, but wants the daemon to run under a normal user account. child_STDIN Defines the path to STDIN for your daemon. Defaults to "/dev/null". Default Mode is '<' (read). You can define other Mode the same way as you do using Perls "open" in a two-argument form. child_STDOUT Defines the path where the output of your daemon will go. Defaults to "/dev/null". Default Mode is '+>' (write/read). You can define other Mode the same way as you do using Perls "open" in a two-argument form. child_STDERR Defines the path where the error output of your daemon will go. Defaults to "/dev/null". Default Mode is '+>' (write/read). You can define other Mode the same way as you do using Perls "open" in a two-argument form, see example below. dont_close_fh If you define it, it must be an arrayref with file handles you want to preserve from the parent into the child (daemon). This may be the case if you have code below a "__DATA__" token in your script or module called by "use" or "require". dont_close_fh => [ 'main::DATA', 'PackageName::DATA', $my_filehandle, ... ], You can add any kind of file handle to the array (expression in single quotes or a scalar variable), including 'STDIN', 'STDOUT' and 'STDERR'. Logically the path settings from above ("child_STDIN", ...) will be ignored in this case. DISCLAIMER: Using this argument may not detach your daemon fully from the parent! Use it at your own risk. dont_close_fd Same function and disclaimer as "dont_close_fh", but instead of file handles you write the numeric file descriptors inside the arrayref. pid_file Defines the path to a file (owned by the parent user) where the PID of the daemon process will be stored. Defaults to "undef" (= write no file). exec_command Scalar or arrayref with system command(s) that will be executed by the daemon via Perls "exec PROGRAM_LIST". In this case the child will never return to the parents process! Example: my $daemon = Proc::Daemon->new( work_dir => '/working/daemon/directory', child_STDOUT => '/path/to/daemon/output.file', child_STDERR => '+>>debug.txt', pid_file => 'pid.txt', exec_command => 'perl /home/my_script.pl', # or: # exec_command => [ 'perl /home/my_script.pl', 'perl /home/my_other_script.pl' ], ); In this example: o the PID of the daemon will be returned to $daemon in the parent process and a pid-file will be created at "/working/daemon/directory/pid.txt". o STDOUT will be open with Mode '+>' (write/read) to "/path/to/daemon/output.file" and STDERR will be open to "/working/daemon/directory/debug.txt" with Mode '+>>' (write/read opened for appending). o the script "/home/my_script.pl" will be executed by "perl" and run as daemon. Therefore the child process will never return to this parent script. METHODS
Init( [ { %ARGS } ] ) Become a daemon. If used for the first time after "new", you call "Init" with the object reference to start the daemon. $pid = $daemon->Init(); If you want to use the object reference created by "new" for other daemons, you write "Init( { %ARGS } )". %ARGS are the same as described in "new". Notice that you shouldn't call "Init()" without argument in this case, or the next daemon will execute and/or write in the same files as the first daemon. To prevent this use at least an empty anonymous hash here. $pid = $daemon->Init( {} ); @pid = $daemon->Init( { work_dir => '/other/daemon/directory', exec_command => [ 'perl /home/my_second_script.pl', 'perl /home/my_third_script.pl' ], } ); If you don't need the Proc::Daemon object reference in your script, you can also use the method without object reference: $pid = Proc::Daemon::Init(); # or $pid = Proc::Daemon::Init( { %ARGS } ); "Init" returns the PID (scalar) of the daemon to the parent, or the PIDs (array) of the daemons created if "exec_command" has more then one program to execute. See examples above. "Init" returns 0 to the child (daemon). If you call the "Init" method in the context without looking for a return value (void context) the parent process will "exit" here like in earlier versions: Proc::Daemon::Init(); Status( [ $ARG ] ) This function checks the status of the process (daemon). Returns the PID number (alive) or 0 (dead). $ARG can be a string with: o "undef", in this case it tries to get the PID to check out of the object reference settings. o a PID number to check. o the path to a file containing the PID to check. o the command line entry of the running program to check. This requires Proc::ProcessTable to be installed. Kill_Daemon( [ $ARG [, SIGNAL] ] ) This function kills the Daemon process. Returns the number of processes successfully killed (which mostly is not the same as the PID number), or 0 if the process wasn't found. $ARG is the same as of "Status()". SIGNAL is an optional signal name or number as required by Perls "kill" function and listed out by "kill -l" on your system. Default value is 9 ('KILL' = non-catchable, non-ignorable kill). Fork Is like the Perl built-in "fork", but it retries to fork over 30 seconds if necessary and if possible to fork at all. It returns the child PID to the parent process and 0 to the child process. If the fork is unsuccessful it "warn"s and returns "undef". OTHER METHODS
Proc::Daemon also defines some other functions. See source code for more details: OpenMax( [ $NUMBER ] ) Returns the maximum file descriptor number. If undetermined $NUMBER will be returned. adjust_settings Does some fixes/adjustments on the "new" settings together with "fix_filename". fix_filename( $KEYNAME ) Prevents double use of same filename in different processes. get_pid( [ $STRING ] ) Returns the wanted PID if it can be found. get_pid_by_proc_table_attr( $ATTR, $MATCH ) Returns the wanted PID by looking into the process table, or "undef". Requires the "Proc::ProcessTable" module to be installed. NOTES
"Proc::Daemon::init" is still available for backwards capability. AUTHORS
Primary-maintainer and code writer until version 0.03: o Earl Hood, earl@earlhood.com, http://www.earlhood.com/ Co-maintainer and code writer since version 0.04: o Detlef Pilzecker, http://search.cpan.org/~deti/, http://www.secure-sip-server.net/ CREDITS
Initial implementation of "Proc::Daemon" derived from the following sources: o "Advanced Programming in the UNIX Environment" by W. Richard Stevens. Addison-Wesley, Copyright 1992. o "UNIX Network Programming", Vol 1, by W. Richard Stevens. Prentice-Hall PTR, Copyright 1998. PREREQUISITES
This module requires the "POSIX" module to be installed. The "Proc::ProcessTable" module is not essentially required but it can be useful if it is installed (see above). SEE ALSO
perl(1), POSIX, Proc::ProcessTable COPYRIGHT
This module is Copyright (C) 1997-2011 by Earl Hood and Detlef Pilzecker. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.12.3 2011-06-04 Proc::Daemon(3pm)
All times are GMT -4. The time now is 07:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy