Everything in the following script works fine, except the jobs command. It works when the script is not running, but does not work in the script itself and I cannot figure out why. Any help would be greatly appreciated.
using bash shell
clear
while
echo " A: Jobs"
echo " B: PS"
echo " C: Kill Process"
echo " Q: Quit"
echo "Please select an option and press <Enter>"
read option
do
case "$option" in
A|a) jobs -l
;;
B|b) ps -l
;;
C|c) echo "Please enter process number to kill"
read process
kill -9 "$process"
;;
Q|q) exit 0
;;
*) echo "Not a valid option"
;;
esac
echo "Press Enter to continue..."
read -p "$*"
more
clear
done
Thank you in advance
Source: jobs(1) [linux man page]
By invoking a shell script, you're basically starting a new session, thus you cannot see the jobs from the previous session.
Actually, your code is working. I added a sleep
command on top of your script for a test (sleep command is executed in the background), and it worked:
sleep 60 & >/dev/null 2>&1
clear
...
...
...
$ bash menu.sh
...
A: Jobs
B: PS
C: Kill Process
Q: Quit
Please select an option and press <Enter>
a
[1]+ 7787 Running sleep 60 &
Press Enter to continue...
Hope this helps.
That worked to show my jobs, however the inode number seemed to be different then when I ran the jobs -l command outside of the script, and It would not Kill it when I entered the inode with option C of the script.
What ended up working was a space . space before I ran the filename. For example, my filename is script5_sysmenu :
. script5_sysmenu
opposed to just
script5_sysmenu