skip to Main Content

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


  1. The following command

    sudo ifconfig enp0s25 >> file
    

    would not work if file requires root privilege to be modified.
    sudo affects ifconfig but not the redirection.

    To fix it:

    sudo sh -c 'ifconfig enp0s25 >> file'
    
    Login or Signup to reply.
  2. As mentioned in Egor Skriptunoff’s answer, sudo only affects the command being run with sudo, and not the redirect.

    Perhaps nothing is being written to file in your case because ifconfig 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:

    sudo sh -c 'ifconfig enp0s25 >> file 2>&1'
    

    Here, sh is invoked via sudo so that the redirect to file will be done as root.

    Without the 2>&1, only ifconfig‘s stdout will be appended to file. The 2>&1 tells the shell to redirect stderr to stdout.

    If file can be written to without root, this may simplify to

    sudo ifconfig enp0s25 >> file 2>&1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search