LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-18-2022, 04:02 PM   #1
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Rep: Reputation: 0
Question Unable to use sudo in my cron script (non-root user)


Environment: RHEL7.9
Access: No root access. I have sudo access to some commands (no password)
Issue: my personal crontab calls a script with sudo, sudo isn't working.


I have a script.sh that calls sudo /usr/systemctl start service, it works from my SHELL but not from crontab -e.

This works: [mike@host] $ sudo /usr/systemctl start service
This works: [mike@host] $ ./script.sh #which has the command it

This fails: [mike@host] $ crontab -e

#MQ CRONS
SHELL=/bin/bash
*/15 * * * * bash -l -c '/export/local/mike/script.sh >> /tmp/log.log'


In more detail, the script runs fine and does every step. When it gets to the sudo command it just doesn't do it or something. Any ideas? is there a cron limitation? I tried source /path/to/mike/.bash_profile in the script and some cron things to no avail.
 
Old 04-18-2022, 05:33 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,617

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
If you read the documentation (man pages) on sudo you will find that it does detect if it is running in interactive mode or not and behaves based upon the call and the mode. There are special parameters to use in scheculed scripting, or you could use a different escalation tool.

Did you turn on shell debugging in the script and log the run? Doing so should have resulted in more information you might have shared.
Did you check the sudo logs or system logs for sudo entries at execution time? There should be a trace of the call there, along with a more or less informative error entry.
 
Old 04-18-2022, 07:51 PM   #3
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Sadly debug output doesn't seem to help me. It rolls right through the script as it should. Basically it checks 3 preconditions and if they all are met etc. it just runs the sudo command last. I will dig into the sudo command a bit more.. what a PITA.

Debug:
+ echo 'Found [3] other servers, enough to continue with DB Start attempt on server01.'
+ sleep 2
++ date +%s
+ now=1650329226
+ get_time_stamp /tmp/TIME_STAMP_FILE.dat
+ file=/tmp/TIME_STAMP_FILE.dat
+ db_check_wait=0
+ '[' -f /tmp/TIME_STAMP_FILE.dat ']'
+ IFS=
+ read -r line
+ previous=0
+ delta=1650329226
+ echo 'now:1650329226 - previous:0 = delta:1650329226'
+ '[' 1650329226 -lt 3600 ']'
+ echo 'restarting => sudo /usr/bin/systemctl start mysql.service.'
+ sudo /usr/bin/systemctl start mysql.service
++ date
+ echo 'DB restart attempt made - Mon Apr 18 20:47:06 EDT 2022'
 
Old 04-18-2022, 10:04 PM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,617

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
And was there a sudo log entry for that command?
 
Old 04-19-2022, 12:17 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
In addition to the above questions, are you starting or restarting the database? The comment says restarting but the actual command says you are trying to start it instead.
 
Old 04-19-2022, 12:42 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can check (in your shell script) if the sudo command was successful (see exit code).
How do you know it is failed?
 
Old 04-19-2022, 09:55 AM   #7
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
And was there a sudo log entry for that command?
Of course, the command works from the command line and when I run the script manually. Thanks for that quick thought but I have no issues with from a terminal.

Last edited by nycmitch50; 04-19-2022 at 10:02 AM.
 
Old 04-19-2022, 10:01 AM   #8
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
In addition to the above questions, are you starting or restarting the database? The comment says restarting but the actual command says you are trying to start it instead.
As stated in the original post, the script and command work when I am running them in a terminal so no issues there. It is only when I put it in cron that it goes to hell.

I have written a lot of bash scripts, this is doing the following:

Start if stopped:

1 Check if the local port 3306 is not open and other things, if that fails.
2 Then check if the cluster size is still at least size two (or do nothing)
3 If it is then check when the last time this script was run, if it were < 1 hour do nothing.

4 If it gets through all of that, the final step is to attempt a start
sudo systemctl start mysql.service


So I can remove the script entirely and just run the start command in cron if that will help people understand the issue.

This is RHEL7.9, there may be something else going on:
https://unix.stackexchange.com/q/122616/108618
 
Old 04-19-2022, 10:19 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I believe wpeckham hit the nail on the tty. Your system is probably configured to require a tty to run sudo commands and cron is not associated with a terminal.

I believe your admin might have to add the following to the sudoers file
Code:
Defaults:your_username !requiretty
As an aside question why do you need to restart mysql?
Or why is it stopping?

Last edited by michaelk; 04-19-2022 at 10:44 AM.
 
1 members found this post helpful.
Old 04-19-2022, 11:18 AM   #10
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,617

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by nycmitch50 View Post
Of course, the command works from the command line and when I run the script manually. Thanks for that quick thought but I have no issues with from a terminal.
That was not the question. The question is if there is a log entry or error code return when the script runs under cron at the point when the sudo command executes. If it is not working, that would give you a clue as to WHY it is not working! If it is not working, it must error and return a code or log the problem, not just die silently.

Last edited by wpeckham; 04-19-2022 at 11:19 AM.
 
Old 04-19-2022, 11:37 AM   #11
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
That was not the question. The question is if there is a log entry or error code return when the script runs under cron at the point when the sudo command executes. If it is not working, that would give you a clue as to WHY it is not working! If it is not working, it must error and return a code or log the problem, not just die silently.
OK I didn't see anything in the log file I was writing and I added the output status to the log, basically the output of the $? of 'sudo'

From cron:
DB restart attempt made (status: 1) - Tue Apr 19 12:08:59 EDT 2022
mysql.service -> NOT RUNNING


From the command line cron:
DB restart attempt made (status: 0) - Tue Apr 19 12:27:55 EDT 2022
mysql.service -> RUNNING

I'm thinking that the limitation is working as designed,

need to get an admin to do something like this: Defaults !requiretty in sudoers

Last edited by nycmitch50; 04-19-2022 at 11:57 AM.
 
Old 04-19-2022, 11:46 AM   #12
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
I believe wpeckham hit the nail on the tty. Your system is probably configured to require a tty to run sudo commands and cron is not associated with a terminal.

I believe your admin might have to add the following to the sudoers file
Code:
Defaults:your_username !requiretty
As an aside question why do you need to restart mysql?
Or why is it stopping?
Hi, I haven't heard anyone say anything about this in the thread except that I posted a link myself wondering if something was going on with that. Thanks for digging into it, I'll have to see what I can get done with the above.


Side topic on the DB:
The DB is a part of a cluster and they do not know why, I was simply providing a possible workaround that would help keep them going until they figured it out. They updated the software and after a period of a day or so 2 dbs will go down, then if not addressed the rest will eventually go and I have to bootstrap the cluster again. So my thought was to write a script that checks on each DB if it is down , check if two others are up and perform a start attempt 1x per time period. A few details need to be cared for such as current cluster size, randomizing the start time, and controlling how often it tries. Noobie scripters to often ignore these things and create underirable scenarios . I can share the script if you wanted.
 
Old 04-19-2022, 02:19 PM   #13
nycmitch50
LQ Newbie
 
Registered: Apr 2022
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks all for your help!

So as an FYI, the issue was with SUDO and TTY

https://www.shell-tips.com/linux/sud...udo/#gsc.tab=0

I had an admin add the following to sudoers: Defaults !requiretty

* this def. has to be done on older RHEL I believe.


I stopped the service and the cron fixed it. Onlyplace sudo is used is in a the command in the script.

Correct output:
DB restart attempt made (status: 0) - Tue Apr 19 15:16:44 EDT 2022
mysql.service -> RUNNING

Last edited by nycmitch50; 04-19-2022 at 02:21 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sudo to user other than root but do not allow sudo to root Westmoreland Linux - Security 4 07-13-2022 01:09 PM
Not able to use sudo command in System calls using non root user mail2manig86 Linux - Embedded & Single-board computer 1 02-20-2019 04:40 AM
'sudo ls /root/monitor/' outputs, 'sudo ls /root/monitor/*' does not stf92 Slackware 10 07-19-2012 05:20 PM
sudo non-user/non-root password Alex_Dc Linux - Security 3 02-19-2010 06:28 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration