Skip to content

Commit f000bf7

Browse files
Process and PID file
1 parent 9433921 commit f000bf7

16 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# print the pud of this script
3+
echo "$$"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# print all processes for all users and TTY displayed
3+
ps -auxf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
# Task 9
3+
terminator () {
4+
rm /var/run/myscript.pid
5+
exit
6+
}
7+
echo "$$" > /var/run/myscript.pid
8+
while true
9+
do
10+
echo "To infinity and beyond"
11+
sleep 2
12+
trap 'echo "Y U no love me?"' SIGINT
13+
trap 'echo "I hate the kill command"; terminator' SIGTERM
14+
trap 'terminator' SIGQUIT
15+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
# Manages the script manage_my_process.
3+
# When passed the argument `start`:
4+
# 1. Starts manage_my_process
5+
# 2. Creates a file containings its PID in /var/run/my_process.pid
6+
# 3. Displays "manage_my_process started"
7+
# When passed the argument `stop`:
8+
# 1. Stops manage_my_process
9+
# 2. Deletes the file /var/run/my_process.pid
10+
# 3. Displays "manage_my_process stopped"
11+
# When passed the argument `restart`:
12+
# 1. Stops manage_my_process
13+
# 2. Deletes the file /var/run/my_process.pid
14+
# 3. Starts manage_my_process
15+
# 4. Creates a file containing its PID in /var/run/my_process.pid
16+
# 5. Displays "manage_my_process restarted"
17+
# If any other or no arguments are passed, displays
18+
#+ "Usage: manage_my_process {start|stop|restart}"
19+
20+
if [ "$1" == "start" ]
21+
then
22+
./manage_my_process &
23+
echo $$ > /var/run/my_process.pid
24+
echo "manage_my_process started"
25+
26+
elif [ "$1" == "stop" ]
27+
then
28+
kill "$(pgrep -f /manage_my_process)"
29+
rm /var/run/my_process.pid
30+
echo "manage_my_process stopped"
31+
32+
elif [ "$1" == "restart" ]
33+
then
34+
kill "$(pgrep -f /manage_my_process)"
35+
rm /var/run/my_process.pid
36+
./manage_my_process &
37+
echo $$ > /var/run/my_process.pid
38+
echo "manage_my_process restarted"
39+
40+
else
41+
echo "Usage: manage_my_process {start|stop|restart}"
42+
fi
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <unistd.h>
5+
6+
/**
7+
* infinite_while - Run an infinite while loop.
8+
*
9+
* Return: Always 0.
10+
*/
11+
int infinite_while(void)
12+
{
13+
while (1)
14+
{
15+
sleep(1);
16+
}
17+
return (0);
18+
}
19+
20+
/**
21+
* main - Creates five zombie processes.
22+
*
23+
* Return: Always 0.
24+
*/
25+
int main(void)
26+
{
27+
pid_t pid;
28+
char count = 0;
29+
30+
while (count < 5)
31+
{
32+
pid = fork();
33+
if (pid > 0)
34+
{
35+
printf("Zombie process created, PID: %d\n", pid);
36+
sleep(1);
37+
count++;
38+
}
39+
else
40+
exit(0);
41+
}
42+
43+
infinite_while();
44+
45+
return (EXIT_SUCCESS);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
# Display the list of pid of current running processes
3+
# shellcheck disable=SC2009
4+
ps -aux | grep bash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# Display the process with their name and id (bash)
3+
pgrep bash -l
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
# print to infinity and beyond infinitely
3+
i=0
4+
while [ $i -eq 0 ]
5+
do
6+
echo "To infinity and beyond"
7+
sleep 2
8+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# termiante a running while loop
3+
kill "$(pgrep -f 4-to_infinity_and_beyond)"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# stop while loop without kill and killall
3+
pkill -f "4-to_infinity_and_beyond"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# stop while loop without kill and killall
3+
pkill -f "7-highlander"
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
# print to infinity and beyond infinitely
3+
i=0
4+
while [ $i -eq 0 ]
5+
do
6+
echo "To infinity and beyond"
7+
sleep 2
8+
trap 'echo "I am invincible!!!"' 15
9+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
# kill the highlander
3+
pkill -f -SIGKILL "7-highlander"

0x05-processes_and_signals/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Linux, processes, siganls and management

0x05-processes_and_signals/a

16.5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
# Indefinitely writes "I am alive" to the file /tmp/my_process.
3+
# Pauses two seconds in between each message.
4+
5+
while true
6+
do
7+
echo "I am alive!" >> "/tmp/my_process"
8+
sleep 2
9+
done

0 commit comments

Comments
 (0)