How to stop a zsh script from being suspended (tty output) - linux

I have a zsh script that I want to run such that it also loads up my .zshrc file.
I believe I have to run my script in interactive mode?
Thus, my script begins like:
#!/bin/zsh -i
if [ $# = 0 ]
then
echo "need command line paramter..."
exit
fi
However, when I try to run this script in the background, my script becomes suspended (even if I pass in the correct number of parameters):
[1] + suspended (tty output)
My question is: How can I make a script that can run in the background that also loads my startup .zshrc file? If I have to put it into interactive mode, how can I avoid the suspension on tty output problem?
Thanks

Don't use interactive mode as a hash-bang!
Instead, source your zshrc file in the script if you want it:
#!/bin/zsh
source ~/.zshrc
...
For future reference, you can use the disown bultin to detach a previously backgrounded job from the shell so it can't be suspended or anything else. The parent shell can then be closed with no affect on the process:
$ disown %1
You can do this directly from the command line when you start the program by using the &! operator instead of just &:
$ ./my_command &!

Related

linux "enable -n xxx" command works in terminal but not when put into a script

I've found a very strange issue, when in linux terminal I type "enable -n trap", it would disable the trap linux builtin command. But if I put it into a script like
#!/bin/bash
enable -n trap
and then run the script, there's no error but the command is also not disabled. Really appreciate if someone could share what is happening and how to run it in some file instead of directly in the terminal. Thank you!
The enable command only affects the current shell. When you run a script, that script is executed in a new process, so:
A new shell starts
The enable command runs and disables the trap command in that shell
The shell exits
If you want to affect the current shell, your only option is to source the script using the . (or source) command. If the script is named disable-trap.sh and is in your $PATH, you can run:
. disable-trap.sh
You can also provide a full path to the script:
. /path/to/disable-trap.sh
Sourcing a script like this is largely equivalent to typing the same commands in at the command line: it executes the instructions in the script in the current shell, rather than spawning a new process.

Bash function to automatically run a command in background and disown

I'm trying to make a function in my bashrc that would allow me to launch any command and automatically disown it.
e.g. launch ./myprogram or launch xdg-open myfolder
I've been used to do that many times command ; Ctrl+Z ; bg ; disown and would like to simply create a shortcut of these steps.
However I don't know how to embed the action of Ctrl+Z in a bash script. I've seen that its action is SIGTSTP, but I'm really lost as to how incorporate that in a bash function.
You can run the command in background directly instead of stopping it and then running it in the background. Use the &:
$ cat > launch
#! /bin/bash
"$#" & disown
Ctrl + d
$ chmod u+x ./launch
For posterity and othe people passing by, here is the bash function I made :
launch()
{
"$#" > /dev/null 2>&1 & disown
}
"$#" takes every arguments given in the prompt as one
> /dev/null 2>&1 redirects every output (stout and stderr) to dev/null which effectively delete them automatically, so that it doesn't appear on the shell
& runs the command in background, meaning it will let you input other commands in the shell
disown , as the name implies will lake it so that the process is no longer bound to the shell and you cans safely close the shell without it closing the process at the same time.

Open new terminal window and execute bash file

I have written a bash script 'A' that executes some commands,but after complete execution of script 'A',I want another bash script 'B' which is stored on desktop to be executed new terminal window after execution of 'A' is completed.What command should I write at end of script 'A'?So that script 'B' is executed in new terminal window. I have tried many commands such as gnome-terminal and konsole both with their various arguments but I couldn't make it work.
Say two commands instead of Script A and Script B I use ls -lh and echo
xterm -e "ls -lh; read ;xterm -e \"echo \"Hello_World\"; read \""
read is given just to stimulate human interaction and you can see the terminal appearing and closing.

Make shell command from child process execute in parent (to lock shell)

I have a user defined command called lock. After executing the command, the system will be locked. If we give only the correct password, then only the command
prompt occurs. Now, I want to execute the lock command from the script which is
running in background. If we execute the script lock command is executed, But it does not lock the current terminal. It locks only the background bash. It does not executed in current bash. How to solve this problem?
Thanks.
You can execute a command before a command is executed from the command prompt using the DEBUG trap:
trap 'command' DEBUG
This will execute command before each command. Change you background script to create a flag file of some kind when the system is locked. And make your command check if that file exists. Like this:
if [[ -f lockfile ]]
then p=
while [[ "$p" != secret ]]
do echo -n password:
read p
done
rm -f lockfile
fi
I am not saying it is secure.
Another method:
Run two shells. The lower shell loops asking for a password and then runs the upper. The upper shell is a normal shell. The background task kills the upper shell when it wants to lock the terminal, returning control to the lower shell.

Launch gdb automatically on Linux

Is there a way to automatically start a process under gdb on Linux? An equivalent of setting the Image File Execution Options on Windows.
I am trying to debug start-up phase of a process that is launched from another one.
I would normally move the real program out of the way, and replace it with a script that launches the program under GDB with the same parameters.
#!/bin/bash
exec gdb -args <realprog> "$#"
If that doesn't work due to the output being redirected to file, or something, then try this:
#!/bin/bash
exec xterm -e gdb -args <realprog> "$#"
That should give you a pop-up terminal with GDB running inside.
You don't have to go through all that registry voodoo on Linux :)
Simply:
1) Rename your program
2) Write a shell script that calls gdb with your (renamed) program and passes any arguments you want. Make sure you "chmod +rx" your script.
3) Name the shell script the original name of your program, and put it in the same directory as your program
4) Execute!

Resources