Linux: Run script in bin with sudo - linux

So I'm sure this is written somewhere, and I'm just wording my searches wrong, but I haven't been able to find it.
I write shell scripts. Many of those scripts require superuser permissions. Rather than putting the sudo command inside the script, I want to invoke the entire script from the start with sudo.
So instead of:
#!/bin/bash
# Sample Script 1
sudo mkdir /usr/tempDir
It would be:
#!/bin/bash
# Sample Script 2
mkdir /usr/tempDir
And I would just invoke it like this: sudo ./sampleScript.sh
My problem is that when I write a script, I simlink it to my bin, so I don't need to worry about the path to the script when I execute it.
If I have the file simlinked to my bin, and I run it as just: sampleScript.sh, it is recognized (and will fail). However, if I do: sudo sampleScript.sh, I get a "sudo: sampleScript.sh: command not found" message.
This happens if it's in both /home/myName/bin and /usr/local/bin.
I know there has to be a trick to making this work that I'm missing. And I also know I'm probably just not wording my google searches properly. Just looking for some guidance to figure this part out.
Thanks.

When you call sudo the PATH changes ie it's not using yours. Look here
sudo changes PATH - why?
It does this because it's now running as root, not as your user.

Related

#reboot crontab has no effect

I have this crontab #reboot "/home/pi/Desktop/TV Scraper 2.0/run.sh" set up and for whatever reason it doesn't seem to run the bash file on reboot.
Typing "/home/pi/Desktop/TV Scraper 2.0/run.sh" on the terminal actually runs the script, so I know it's correct.
This is what's inside run.sh just in case:
#!/bin/bash
cd "/home/pi/Desktop/TV Scraper 2.0"
node ./app.js
I've also tried using #reboot root sh "/home/pi/Desktop/TV Scraper 2.0/run.sh" as well, but it doesn't work either.
How can I move forward with this? My knowledge of Linux is very limited. All I need is to have some Node and Python3 scripts run on every reboot. On Windows that's such an easy task: I've tried CRON, rc.local and autostart, nothing works.
My guess is that node is not available via cronjob, since its containing directory is not in your PATH environment variable. When you execute the script manually, it's probably available via PATH.
An easy fix for this is to use the full path, which you can get by executing which node. The result should be something like /usr/bin/node. Then you can use that, instead of just node.
For debugging purpose you can also redirect stdout and stderr to a file, so the last line in your script would look like this:
/usr/bin/node ./app.js &>/tmp/cron-debug.log
If that doesn't fix it, i would rename the directory "TV Scraper 2.0" and replace the whitespace characters with something like an underscore. Directory and file names are less likely to cause problems if you avoid whitespaces.

Cannot run .sh script under sudo in linux

I have a script foo.sh located in /home/pi/Documents/Python directory. Purpose of this shell script is to run python script which needs root priviledges as it must reset usb device.
The script is as follows:
#!/bin/sh
export PATH="$PATH:/home/pi/.local/lib/python3.7"
python3 /home/pi/Documents/Python/foo.py
When I run the foo.py from Midnight Commander (setting a cursor on the file and pressing enter) it works, it exports the path correctly and the python script fails as it does not have enough priviledges to reset usb device.
I have actually made this script to run python script under root, but the root needs set a path to used module first.
However when I run
sudo foo.sh
I receive an answer:
sudo: foo.sh: command not found
I have checked the permissions and the foo.sh file has -rwxr-xr-x
sudo python3
typed in terminal also works correctly and opens python interpreter.
What is the problem that causes wrong behaviour under sudo?
I might be mistaken (I don't have a Linux Machine at hand atm, so I cannot verify), but if I recall correctly the user_home is part of the PATH variable exported for that user.
When you use the command sudo you are acting on the behalf of root which has got a different user_home than yours (== the current user), therefore your script is not found in any of the directories listed in the active PATH (the one of root because you are using the sudo command).
However, it should be possible to run successfully the following command:
$ sudo ./foo.sh
I hope this might shed some light.
Unless foo.sh is in a directory shown referenced by the PATH environmental variable, the environment will not recognise the command and hence the error
If you are in the directory with the foo.sh script, execute it with:
sudo ./foo.sh
If you are in a different directory, execute with:
sudo /pathtosh/foo.sh

lzma command not found when executing shell script only under sudo

I am building project source code in a SUSE server.
The project build.sh called "lzma" command to compress kernel.
The project build.sh need "sudo" to get access to some system command.
But I has tried to execute "sudo ./build.sh", and the shell always report error: "lzma: command not found."
I could execute "lzma" in shell with my user account. It works fine.
I also write a test shell script named "test.sh" which calls "lzma" command.
I found that it fails with same error message if I excute "test.sh" with "sudo" .
But if I execute "test.sh" without "sudo", it works fine.
Why ?
"Command not found" within sudo is almost invariably the result of an environment variable such as PATH, LD_LIBRARY_PATH (if what's missing is not the executable but a shared library it requires) or the like being altered.
You can pass working values through your environment variables through explicitly:
sudo PATH="$PATH" ./test.sh
Sudo uses a different Path then your user account.
EDIT (see comments)
Try and execute:
type lzma
Say the output reads something like '/usr/bin/lzma', then just copy that output into your sudo command like (for example):
sudo /usr/bin/lzma
That should do the trick. You should also write the full path of lzma into your shell script if you are to run it as root.
EDIT 2:
Or, as Charles Duffy mentioned in his answer, you could leave all things as is and simply use PATH="$PATH" in your command if you are trying to execute your file as SUDO or as a different user.

I'm learning about shebangs. How do I make it work with node.js in a Mac terminal?

I have:
#!/usr/bin/env node
console.log("It works!");
I learned that env finds the node program and interprets it with node. I checked that env exists in /usr/bin.
When I call node itworks.js it works and outputs It works!. However, from what I understand, I should just be able to call itworks.js without node due to the shebang. But when I make this command it says -bash: itworks.js: command not found.
Could someone help me get the shebang to work?
First of all you need to make the file executable:
chmod +x itworks.js
Then you need to call it by specifying the path as well. Either:
/where/it/is/on/disk/itworks.js
or:
./itworks.js
The reason for :
-bash: itworks.js: command not found
is because bash looks for programs in directories in the PATH environment variable when you do not say where the file is - it does not look in the current directory unless you tell it.
You could update the PATH variable with the current directory shortcut ., but that can be a security risk, so most run the program like this:
./itworks.js
Of course if you put your scripts all in one directory then you could add that to PATH in one of your start-up files. For example, if you had a directory called bin in your home directory that held your scripts:
PATH=$PATH:"$HOME/bin"
You also need to add the execute permissions to the script:
chmod u+x itworks.js
The u indicates that we only give permission for the current user to execute this file. If we omit the u then anyone can run it.

Execute Script using an Alias

I am trying to create an alias that will execute a script. When i cd into the directory where the script is located... lets say /usr/local/bin/startscript then the script runs as expected and starts the application i want it to.
SO. i went into my bashrc file and added an alias
alias startscript='/usr/local/bin/startscript'
The goal is to be able to run the script by simply typing "startscript" from any directory.
However, when i try to use the alias to run the script, it does not work properly as the application that should start, does not.
My script starts with
#!/bin/sh
and then goes from there
any ideas? Thanks
SCRIPT:
#!/bin/sh
#- Check for user 'user'
if [[ "`whoami`" != "user" ]]; then
echo "This script can only be executed by user 'user'."; exit
fi
. /usr/local/bin/etctrx/startscriptdirectory/startscriptsetup
#- Kill manager to avoid multiple processes
pkill -f 'JavaApp.jar'
#- Start
nohup java -classpath /usr/local/bin/etctrx/startscriptdirectory/RequiredJars/ojdbc5.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/activation.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/mail.jar -jar /usr/local/bin/etctrx/startscriptdirectory/JavaApp.jar > ${JAVAAPPLOGS}/startscript.log 2>&1 &
If the script runs as expected while in /usr/local/bin by simply typing startscript, but from another directory the script runs (does not return an error), but doesn't produce the desired results, then the issue is with how you reference the application from within the script.
As others have noted, you shouldn't need an alias for something in /usr/local/bin and if it runs from that directory, obviously your executable permissions are correct too. If the application you're trying to run is also in /usr/local/bin then your script probably assumes it's in the same directory, which wouldn't be the case elsewhere, so you would need to either ad a cd to /usr/local/bin within the script or specify the full application path.
I am able to call the script if i do this, but it still won't give me the
results I want,(application being started) like i do when I run the script from
the directory it lives in
It would appear that the "application" in question is in the same directory as the script, /usr/local/bin, which we have established is already on your PATH. For the script to run correctly but not the application means you might be calling the application wrong, for example
./application
This would fail unless you were calling from /usr/local/bin. Fix would be like this
application

Resources