Linux, BASH launch command with special char [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I must implement a deploy script that launches command by special char from specific user.
The command that I must launch is:
cd /path
. ./setantenv.sh
I am trying to launch the command with this syntax:
su - USER -s /bin/bash -c 'cd /PATH/ && . ./setantenv.sh'
su - USER -s /bin/bash -c 'cd /PATH/ && ant clean all'
But I've got a problem with . ./setantenv.sh

In order to configure the environment for your subsequent ant command, you have to include the ". ./setantenv.sh" inside your second call. Both calls result in independent bash processes that dont share their specific environment.
try this:
su - USER -s /bin/bash -c 'cd /PATH/ && . ./setantenv.sh && ant clean all'

Related

Responding to a sudo password request in a script? [duplicate]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

crontab doesn't work after /etc/crontab is configured [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 1 year ago.
Improve this question
I added this line to the end of /etc/crontab file:
* * * * * cp /etc /backups -R > /dev/null && tar -Jcf /backups/etc-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.tar.xz /backups/etc > /dev/null && rm -rf /backups/etc > /dev/null
and then I restarted the crond service with systemctl restart crond command
but didn't work
It worked when I run this command cp /etc /backups -R > /dev/n...... in terminal
And my mailx for root is empty.
Could anyone tell me whats wrong with my configuration?
Thank you very much!!!
/etc/crontab is not an ordinary crontab file; it's a system crontab file. Each line has the usual 5 fields specifying the schedule, then a 6th field specifying the account. With the line you show in /etc/crontab, it will attempt to run the command as user cp.
If you're using the Vixie cron implementation (you probably are), run man 5 crontab and search for "EXAMPLE SYSTEM CRON FILE".
I recommend not touching /etc/crontab. Rather use the crontab command to create a user crontab for whichever account you want. Run crontab as root if the command begin run needs root access.
I think now its fine
[root#localhost public]# crontab -e
crontab: installing new crontab
[root#localhost public]# systemctl restart crond
[root#localhost public]# crontab -l
* * * * * cp /etc /backups -R > /dev/null && tar -Jcf /backups/etc-`date +\%Y-\%m-\%d- \%H-\%M-\%S`.tar.xz /backups/etc > /dev/null && rm -rf /backups/etc > /dev/null
[root#localhost public]#

werid behavior of sudo [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
In my linux machine, path are configured as follows
non-root user:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java
root user:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
when i tried to
sudo echo $PATH
it shows non-root user path only not root path
but when i put
echo $PATH
in script and tried to execute with sudo, it gives root path. Do anyone knows this reason? Actually sudo is for executing command as root but in my first case it is not working fine.
sudo echo $PATH executes echo $PATH as root in the current non-root environment, i.e. with the non-root value of $PATH. When you do sudo bash somescript.sh, bash is executed as root and during its initialization, it loads the root environment containing root's value of $PATH.
When you run sudo echo $PATH, the shell expands $PATH before ever calling sudo, so you are really running this:
sudo echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java
which gives the results you are seeing - the non-root user PATH being displayed.
When echo $PATH is embedded in a script and you do sudo somescript.sh, the shell that is running the script runs as root, and so when it expands $PATH as part of interpreting the script, it sees root's environment and displays the root version of the PATH.
In order to avoid the early expansion in the first case, you could do this:
sudo bash -c 'echo $PATH'
assuming your sudoers is set up to allow that. The single quotes prevent the non-root users shell from expanding the variable before passing it as a command to bash.

Use sudo with password as parameter [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

How do I add a user in Ubuntu? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Specifically, what commands do I run from the terminal?
Without a home directory
sudo useradd myuser
With home directory
sudo useradd -m myuser
Then set the password
sudo passwd myuser
Then set the shell
sudo usermod -s /bin/bash myuser
Here's the command I almost always use (adding user kevin):
useradd -d /home/kevin -s /bin/bash -m kevin
There's basicly 2 commands to do this...
useradd
adduser (which is a frendlier front end to useradd)
You have to run them has root.
Just read their manuals to find out how to use them.

Resources