Im trying to run my .sh scipt status.sh via a telegram message:
Ubuntu 20.04.1 LTS server
Telegram-cli with a lua script to action status.sh script
when i send the message "status" to my server via telegram it actions the status.sh script, in this script i have a bunch of stuff that gathers info for me and sends it back to telegram so i can see what the status of my server is, however (i recently did a fresh install of the server) for some reason if the script has a line of code starting with sudo i get:
line 38: /usr/bin/sudo: Permission denied
if i run the script from the command line ./status.sh it runs without any problem!? so im thinking its because it is being called from telegram or lua!?
example of code that generates the error: sudo ifconfig enp0s25 >> file
on the other hand this line works without a problem:
sudo echo Time: $(date +"%H:%M:%S") > file
/usr/bin has 0755 permission set
sudo has 4755 permission set
2
Answers
The following command
would not work if
file
requires root privilege to be modified.sudo
affectsifconfig
but not the redirection.To fix it:
As mentioned in Egor Skriptunoff’s answer,
sudo
only affects the command being run withsudo
, and not the redirect.Perhaps nothing is being written to
file
in your case becauseifconfig
is writing the output you are interested in to stderr instead of to stdout.If you want to append both stdout and stderr to
file
as root, use this command:Here,
sh
is invoked viasudo
so that the redirect tofile
will be done as root.Without the
2>&1
, onlyifconfig
‘s stdout will be appended tofile
. The2>&1
tells the shell to redirect stderr to stdout.If
file
can be written to without root, this may simplify to