Is there a way to 'inline' a block of code the run in the background without defining the block as a function? I was thinking something like:
( do something; a bit more; finally this ) &
( more things; etc ...; ) &
wait
proceed ...
I suppose it's only one line extra define a single-use function and then immediately use it but I was curious and didn't find anything searching.
Use bg to Send Running Commands to the Background You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background. You can view a list of all background tasks by typing jobs in the terminal.
To send a running command in the background: If users have already started a certain command and while they were using their system, their command-line blocks up, then they can suspend the execution of their currently foregrounded process by using “ctrl+z” for windows and “command+z” for mac systems.
There are two ways to implement Bash functions: Inside a shell script, where the function definition must be before any calls on the function. Alongside other bash alias commands and directly in the terminal as a command.
#!/bin/sh
{
echo "sleeping for 5 seconds"
sleep 5
echo "woke up"
} &
echo "waiting"
wait
echo "proceed"
$ ./bgblock
waiting
sleeping for 5 seconds
woke up
proceed
I could do it with something like this:
#!/bin/sh
{
echo "sleeping for 5 seconds"
sleep 5
echo "woke up"
} 1>/dev/null 2>&1 &
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With