Wednesday 9 December 2015

Process Management-Shell Scripting Linux


Process-management-Linux-Shell-Scripting
 Q:-Write a shell script for Process Management in Linux. Provide facility to create a process, display running process and to change the priority of the process.

 

 

 

 Program 

  1. Language:-Bash
  2. Functions are used to implement the various facilities.
  3. Menu is implemented using case statement
  4. To increase the priority i.e, reduce the nice value or creating a process with negative nice value, some times sudo/su needs to be used. So if u get any error while doing so, try running the script as root.

 

Source Code


#!/bin/bash
function runprocess
{
ps -eo pid,pri,nice,command
}
function createprocess
{
echo "Enter the nice value"
read niceval
echo "Enter the name of the process to create"
read proname
nice -n $niceval $proname &
echo "process has been created"
}
function setpriority
{
echo "Enter the pid value of the process whose priority is to be changed!"
read pidno
echo "Enter the new priority value for the process!"
read priorityval
renice $priorityval $pidno
}
while [ true ]
do
echo "Enter your choice"
echo "1.Create new process 2. Display running process 3. To change priority of a process 4. Exit"
read choice
case "$choice" in
1)createprocess
;;

2)runprocess
;;

3)setpriority
;;
4)
echo "terminating script"
exit;;
*)
echo "Invalid choice,Enter again"
esac
done

Sample Output


student@localhost]$ sh asgn6.sh

Enter your choice

1.Create new process 2. Display running process 3. To change priority of a process 4. Exit

1
Enter the nice value
2
Enter the name of the process to create
firefox
process has been created
Enter your choice
1.Create new process 2. Display running process 3. To change priority of a process 4. Exit
2
PID PRI NI COMMAND
1 19 0 /usr/lib/systemd/systemd
2 19 0 [kthreadd]
3 19 0 [ksoftirqd/0]
5 19 0 [kworker/u:0]
6 139 - [migration/0]
7 139 - [watchdog/0]
8 139 - [migration/1]
10 19 0 [ksoftirqd/1]
12 139 - [watchdog/1]
13 39 -20 [cpuset]
14 39 -20 [khelper]
15 19 0 [kdevtmpfs]
16 39 -20 [netns]
17 19 0 [sync_supers]
18 19 0 [bdi-default]
19 39 -20 [kintegrityd]
20 39 -20 [kblockd]
21 39 -20 [ata_sff]
22 19 0 [khubd]
23 39 -20 [md]
26 19 0 [kswapd0]
27 14 5 [ksmd]
28 0 19 [khugepaged]
29 19 0 [fsnotify_mark]
30 39 -20 [crypto]
36 39 -20 [kthrotld]
37 19 0 [kworker/u:1]
38 19 0 [scsi_eh_0]
39 19 0 [scsi_eh_1]
41 19 0 [scsi_eh_2]
42 19 0 [scsi_eh_3]
43 39 -20 [kpsmoused]
314 39 -20 [kdmflush]
316 39 -20 [kdmflush]
361 19 0 [jbd2/dm-1-8]
362 39 -20 [ext4-dio-unwrit]
392 19 0 /usr/lib/systemd/systemd-journald
393 19 0 [kauditd]
396 19 0 /usr/lib/udev/udevd
421 19 0 [flush-253:1]
503 39 -20 [hd-audio0]
526 19 0 [jbd2/sda3-8]
527 39 -20 [ext4-dio-unwrit]
529 39 -20 [kdmflush]
546 19 0 [jbd2/dm-2-8]
547 39 -20 [ext4-dio-unwrit]
552 19 0 [flush-253:2]
567 23 -4 /sbin/auditd -n
569 19 0 /usr/sbin/smartd -n -q never
577 19 0 /usr/sbin/gpm -m /dev/input/mice -t exps2
580 19 0 /usr/sbin/NetworkManager --no-daemon
581 19 0 /usr/sbin/acpid
583 19 0 /usr/sbin/abrtd -d -s
586 19 0 /usr/bin/abrt-watch-log -F BUG corruption stack overflow protection fault WARNING: at nable to handle ouble fault: RTNL: assertion failed eek! p
591 19 0 /usr/lib/systemd/systemd-logind
596 19 0 /usr/bin/system-setup-keyboard
597 19 0 avahi-daemon: running [linux.local]
599 19 0 avahi-daemon: chroot helper
601 19 0 /usr/sbin/mcelog --ignorenodev --daemon --foreground
612 27 -8 /sbin/audispd
614 23 -4 /usr/sbin/sedispatch
616 19 0 /usr/sbin/atd -f
618 19 0 /usr/sbin/gdm-binary -nodaemon
619 19 0 /usr/sbin/crond -n
620 19 0 /bin/dbus-daemon --system --address=systemd: --nofork --systemd-activation
623 19 0 /sbin/rsyslogd -n -c 5
668 19 0 /usr/bin/python -Es /usr/sbin/setroubleshootd -f
673 19 0 /usr/libexec/polkit-1/polkitd --no-debug
677 19 0 /usr/sbin/modem-manager
681 19 0 /usr/libexec/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1
685 19 0 /usr/sbin/bluetoothd -n
692 19 0 /usr/bin/Xorg :0 -background none -logverbose 7 -auth /var/run/gdm/auth-for-gdm-1hx0HO/database -seat seat0 -nolisten tcp vt1
777 19 0 /usr/libexec/accounts-daemon
832 19 0 /usr/libexec/upowerd
882 18 1 /usr/libexec/rtkit-daemon
900 19 0 gdm-session-worker [pam/gdm-password]
915 19 0 /usr/bin/gnome-keyring-daemon --daemonize --login
919 19 0 gnome-session
930 19 0 dbus-launch --sh-syntax --exit-with-session
931 19 0 /bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
993 19 0 /usr/libexec/imsettings-daemon
996 19 0 /usr/libexec/gvfsd
998 19 0 /usr/libexec//gvfs-fuse-daemon -f /run/user/student/gvfs
1002 19 0 /usr/lib/udev/udevd
1003 19 0 /usr/lib/udev/udevd
1094 19 0 /usr/libexec/gnome-settings-daemon
1103 30 -11 /usr/bin/pulseaudio --start
1111 19 0 /usr/sbin/cupsd -f
1114 19 0 /usr/libexec/gsd-printer
1119 19 0 /usr/libexec/gvfs-udisks2-volume-monitor
1121 19 0 /usr/lib/udisks2/udisksd --no-debug
1127 19 0 /usr/libexec/gvfs-gphoto2-volume-monitor
1128 19 0 /usr/libexec/colord
1135 19 0 /usr/bin/gnome-shell
1140 0 19 /usr/libexec/tracker-miner-fs
1141 19 0 abrt-applet
1144 19 0 gnome-screensaver
1149 19 0 /usr/bin/seapplet
1151 19 0 /usr/libexec/tracker-store
1153 19 0 nm-applet
1154 19 0 /usr/libexec/deja-dup/deja-dup-monitor
1155 19 0 /usr/libexec/evolution/3.4/evolution-alarm-notify
1167 19 0 /usr/libexec/dconf-service
1218 19 0 /usr/libexec/gconfd-2
1219 19 0 /usr/libexec/colord-sane
1234 19 0 /usr/libexec/evolution-calendar-factory
1243 19 0 /usr/libexec/evolution-addressbook-factory
1247 19 0 /usr/libexec/goa-daemon
1255 19 0 /usr/libexec/gnome-shell-calendar-server
1257 19 0 /usr/libexec/mission-control-5
1274 19 0 /usr/libexec/libsocialweb-core
1286 19 0 gnome-terminal
1292 19 0 gnome-pty-helper
1293 19 0 bash
1388 19 0 /usr/sbin/sshd -D
1394 19 0 /sbin/rpcbind -w
1398 19 0 sendmail: accepting connections
1399 39 -20 [rpciod]
1406 19 0 /sbin/rpc.statd
1416 19 0 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
1420 19 0 man mount
1431 19 0 less -s
1441 19 0 man umount
1453 19 0 less -s
1475 19 0 /usr/libexec/gvfsd-trash --spawner :1.4 /org/gtk/gvfs/exec_spaw/0
1478 19 0 /usr/libexec/gvfsd-burn --spawner :1.4 /org/gtk/gvfs/exec_spaw/1
1508 19 0 sh asgn5.sh
1547 19 0 /usr/libexec/gvfsd-metadata
1595 19 0 man ps
1607 19 0 less -s
1618 19 0 man ps
1629 19 0 less -s
1644 19 0 man ps
1655 19 0 less -s
1720 19 0 man priority val
1730 19 0 less -s
1736 19 0 man renice
1748 19 0 less -s
1755 19 0 man setpriority
1772 19 0 less -s
1791 19 0 sh asgn6.sh
1834 19 0 /usr/libexec/gvfsd-http --spawner :1.4 /org/gtk/gvfs/exec_spaw/2
1864 19 0 /usr/libexec/at-spi-bus-launcher
1885 19 0 man renice
1896 19 0 less -s
1916 19 0 sh asgn6.sh
1917 19 0 man ps
1928 19 0 less -s
1937 19 0 sh asgn6.sh
1940 19 0 man ps
1951 19 0 less -s
1959 19 0 info ps
1964 19 0 sh asgn6.sh
1971 19 0 man nice
1987 19 0 less -s
2079 19 0 man renice
2090 19 0 less -s
2122 19 0 sh asgn6.sh
2161 19 0 man nautilus
2170 19 0 less -s
2178 19 0 man nice
2192 19 0 less -s
2244 19 0 [kworker/1:0]
2245 19 0 man nice
2259 19 0 less -s
2309 19 0 [kworker/0:1]
2336 19 0 sh asgn6.sh
2407 19 0 nautilus
2415 19 0 gedit /home/student/2101/asgn6.sh
2423 19 0 [kworker/1:1]
2424 19 0 sh asgn6.sh
2461 19 0 [kworker/0:0]
2505 19 0 man kill
2521 19 0 less -s
2528 19 0 [kworker/0:2]
2531 19 0 sh asgn6.sh
2532 19 0 [kworker/1:2]
2533 17 2 /usr/lib64/firefox/firefox
2566 19 0 ps -eo pid,pri,nice,command
Enter your choice
1.Create new process 2. Display running process 3. To change priority of a process 4. Exit
3
Enter the pid value of the process whose priority is to be changed!
2533
Enter the new priority value for the process!
5
2533 (process ID) old priority 2, new priority 5
Enter your choice
1.Create new process 2. Display running process 3. To change priority of a process 4. Exit
4
terminating script
[student@localhost ]$

 

 

No comments:

Post a Comment