You need to save the PID of the background process at the time you start it:
foo &
FOO_PID=$!
# do other stuff
kill $FOO_PID
You cannot use job control, since that is an interactive feature and tied to a controlling terminal. A script will not necessarily have a terminal attached at all so job control will not necessarily be available.
Related Contents:
- Iterate over a list of files with spaces
- How do I test if a variable is a number in Bash?
- Defining a variable with or without export
- What’s the best way to send a signal to all members of a process group?
- How to prevent a background process from being stopped after closing SSH client in Linux
- What does set -e mean in a bash script?
- Is there a way to change the environment variables of another process in Unix?
- What’s the magic of “-” (a dash) in command-line parameters?
- How to remove the lines which appear on file B from another file A?
- Environment variable substitution in sed
- What does “&” at the end of a linux command mean?
- Better way to rename files based on multiple patterns
- Is there a “goto” statement in bash?
- How do I know the script file name in a Bash script?
- Shell script: Run function from script over ssh
- Forcing bash to expand variables in a string loaded from a file
- How can I generate new variable names on the fly in a shell script?
- Is there a way to ‘uniq’ by column?
- Bash script processing limited number of commands in parallel
- How can I delete a newline if it is the last character in a file?
- Redirect STDERR / STDOUT of a process AFTER it’s been started, using command line?
- Get most recent file in a directory on Linux
- Linux PID recycling [closed]
- Get program execution time in the shell
- Retrieve CPU usage and memory usage of a single process on Linux?
- How to handle more than 10 parameters in shell
- How do I exclude a directory when using `find`?
- Raise error in a Bash script
- Bash script process substitution Syntax error: “(” unexpected
- What is the difference between using `sh` and `source`?
- Avoid gnome-terminal close after script execution?
- Shell command to tar directory excluding certain files/folders
- How do you normalize a file path in Bash?
- How to only get file name with Linux ‘find’?
- Exit code of variable assignment to command substitution in Bash
- My shell script stops after exec
- ‘find -exec’ a shell function in Linux
- How to compare two DateTime strings and return difference in hours? (bash shell)
- Programmatically get parent pid of another process?
- How to get file creation date/time in Bash/Debian?
- Round a divided number in Bash
- How to use sed to remove the last n lines of a file
- Using the passwd command from within a shell script
- Get a list of function names in a shell script [duplicate]
- Syntax with pound and percent sign after shell parameter name [duplicate]
- How to kill zombie process
- How to create a link to a directory on linux [closed]
- Randomly shuffling lines in Linux / Bash
- Difference between shell and environment variables
- How to check if sed has changed a file