Automatically run a program on startup under Linux Ubuntu [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I'd need a program to be run every time I start up my Ubuntu Linux. So I'd need to add it to my startup programs list. Just one problem: I'd need to do it via the terminal.

sudo mv /filename /etc/init.d/
sudo chmod +x /etc/init.d/filename
sudo update-rc.d filename defaults
The script should now start on boot. Note that this method also works with both hard links and symbolic links (ln).
At this point in the boot process PATH isn't set yet, so it is critical that absolute paths are used throughout. But, as pointed out in the comments by Steve HHH, explicitly declaring the full file path (/etc/init.d/filename) for the update-rc.d command is not valid in most versions of Linux. Per the manpage for update-rc.d, the second parameter is a script located in /etc/init.d/*.
Also as pointed out in the comments (by Charles Brandt), /filename must be an init style script. A good template was also provided - System V init script template.
As pointed out in the comments (by Russell Yan), this works only on default mode of update-rc.d.
According to the manual of update-rc.d, it can run on two modes: "the machines using the legacy mode will have a file /etc/init.d/.legacy-bootordering", in which case you have to pass sequence and runlevel configuration through command line arguments.
The equivalent argument set for the above example is
sudo update-rc.d filename start 20 2 3 4 5 . stop 20 0 1 6 .

Related

How can I remove scripts that run by default when I open the Linux terminal? (WSL) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I accidentally created a script that runs automatically when I open the linux terminal on windows.
I completely forgot where I created these files and now I am stumped on how to remove them.
Is there anyway I can remove these scripts or have the Linux terminal be reset to default settings?
I am using Bash on Ubuntu on Windows.
Terminal with automatic scripts
What you are asking is how to find-out-what-scripts-are-being-run-by-bash-on-startup. That link answers that question but here is the short of it:
To find all of them you could run:
echo exit | strace bash -li |& grep '^open'
(-li means login shell interactive; use only -i for an interactive non-login shell.)
This will show a list of files which the shell opened or tried to open. On many systems, they are as follows:
Also it's good to know that by default the following are usually run:
/etc/profile
/etc/profile.d/* (various scripts in /etc/profile.d/)
/home/<username>/.bash_profile
/home/<username>/.bash_login
/home/<username>/.profile
/home/<username>/.bashrc
/home/<username>/.bash_history (Not a script. Simply enables history command)
/usr/share/bash-completion/bash_completion
/etc/bash_completion.d/*
/etc/inputrc (defines key bindings; this is not a script)
FYI: /home/<username>/ is the same as ~ on most systems
For each of the scripts mentioned above you may want to check if they are calling yet another script... An easy way to do that is to grep all of those scripts for keywords implying they are calling another script (although strace will already show that)
You may want to:
cat <script_name> | grep -e 'bash' -e 'source' -e '\.\/'
In the WSL terminal, I would try to check if you are calling a startup file in ~/.bashrc or ~/.bash_profile. These are typical dotfiles that are called when a new BASH session is started. In your case, look for a call to /bin/brew (or a line that may potentially call brew) in these files.

How to programmatically trigger actions when last Linux terminal is exited/killed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I've modified my .bashrc to mount directories when I start first terminal after login.
If mount point still exists when I start new terminal nothing is done.
Now I want to add a bit of code when last linux terminal is closed/exited (e.g. umount those directories, etc..)
Also there is this not-so-intelligent way of discovering how many terminal instances are still running:
ps -au | grep "bash" | grep "grep" -v -c
I'm running Ubuntu 20.04. and I'm using bash shell.
Questions:
Is there a file which is "triggered" on terminal exit just like .bashrc is on terminal startup? I've tried messing around with .bash_logout but it doesn't seem to do anything in my case (echo, touch..)
Is there another way to do what I'm trying to achieve which doesn't include file from question #1 (if such file even exists)?
You can get it done with the help of trap which is a shell builtin.
For example if you want to clear a folder on running exit command in bash >
trap "rm /cache/*" EXIT
The syntax should be like trap <command> <SIGNAL>
Just put this in the bottom of ~/.bashrc with your desired command and it should run before the terminal is killed.
Try trap --help to know more.

Run applications with bash from Linux Mint [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I started learning Bash on Mint Linux. The thing is I want to know how to open and execute programs. I have a test.sh in my junk directory so that I can mess around but when ever I type in gnome-open test.sh it just opens the file and not actually run it. In the test.sh file I have echo hi in there so that I can see that it worked and I gave the file the permissions for it to be an executable file so it should execute.
You need to do two things:
Give the file execution permission (+x)
Execute the file
First you give the file permission no 755:
chmod 755 test.sh
Then you start it:
./test.sh
The dotslash means "current directory", it's like saying c:\file.bat if \ is the current directory. You need that because the current dir (called PWD) is not in your PATH variable which means that you either need to specify the complete path, eg. /users/user/file.sh or using the dot which is a shortcut for the current directory.
The file permission number 755 means:
owner: 7 (read, write, exec)
group: 5 (read, exec)
other: 5 (read, exec)
If you want to be the only one to be able to even open the file you may specify 700 instead. There are plenty of combinations, but 755 is most commonly used for scripts.
edit:
I forgot to mention that you need the dotslash everytime you run the script, but you only need to issue the chmod command once for every file.

Telnet to Embedded linux board: command not found [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm trying to telnet to an embedded Linux based device and I'm able to log in successfully using the root credentials. However, I'm not able to run many bash commands.
Commands like mv, cp, cd, ls, etc. are working, but commands like uname, df, install, etc. (even commands required for POSIX compliance!) don't seem to work. Can't seem to figure out what I'm missing.
# install
-sh: install: not found
# uname -1
-sh: uname: not found
# ls
bin etc lib opt sbin tmp utils
dev home mnt proc sys usr var
Usage of Busybox as shell interpreter is very common in embedded world.
It's an ash interpreter that can be compiled with minimum functionality.
Try to determine shell interpreter you have on your system using this tips:
How to determine the current shell I'm working on?
You can "ls /usr/bin" and "ls /usr/sbin" to see what commands are physically available.

sudo E option does not work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
Seems to me that if I write a shell script in $HOME/path/to/script/myscript, and export PATH=$PATH:$HOME/path/to/script/ then sudo -E myscript should run the script right?
From sudo man page:
‑E
The ‑E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the ‑E option is specified and the user does not have permission to preserve the environment.
But sudo -E script can't find the command. I know I can just add /absolute/path/to/script/ to the files that determine the PATH variable for sudo env, but that's not the approach I want to take.
To address the comments:
#H2CO3: I have seen sudo sudo floating around the net, and it bugs me tremendously, it's literally saying "Give me superuser permissions to run superuser". I don't know if it would solve the problem, but it seems like a fundamentally flawed approach. Then again I started using linux/bsd systems back in the mid 90's so I take issue with just the sudo command itself.
#rici: The script does have executable permissions, but that's definitely something to check whenever something doesn't run in *nix systems =)
/etc/sudoers by default has a setting to reset the environment. And also defines a default secure_path which is in effect when you run sudo -E [cmd].
You will need to edit the /etc/sudoers file and add 'env_keep' and mention the variables you want to preserve.
Default env_keep += "PATH"
but before this comment out the secure_path line. Then try your command via sudo -E.

Resources