We calling a PLSQL program unit from shell script, and the PLSQL program unit writes a file in database file system mount location with 644 permission. And then finally the shell script attempts to change the file permission mode to 764 using below statement.
chmod 764 $file
During run time, script is requesting for user input to change the file permission.
override mode 644 on /path/to/file/filename?
How to suppress this or is there any way to provide 'Y' in the chmod command itself ?
To suppress chmod messages you can add -f flag as:
chmod -f 764 $file
-f, --silent, --quiet suppress most error messages
Related
I am running a bash script and these commands in the script will not work without sudo in front of them. The script.sh is located in a folder such as /jobs/script.sh
Example of commands I am trying to run in the script.sh -
mv /var/app/myapp /var/app/myapp.old
rm file.tar.gz
tar -xzf /home/ubuntu/file.tar.gz -C /var/app/
All the above work if I add sudo in front of them.
I am trying to figure out what permissions are required for them to work without adding sudo in the script.
I have tried giving the script.sh rwx permissions and changing owner to root.
I'm learning permissions in linux, so I'm new to this. Basically what permission should the script.sh have so that I dont have to use sudo in the bash file? Any insight would greatly help.
When you run sudo <some command>, then <some command> is run by the root user (Super user do). The reason you might need to run any command using sudo is because the permissions on the files that command reads/writes/executes are such that only the "Super user" (root) has that permission.
When executing the command mv fileA fileB, the executing user would need:
Write permission to fileB if fileB already existed
Write permission to the directory containing fileB
From what you said it’s most likely you want read and write permissions you can achieve this with chmod
Chmod +[permission] filename
(+ is used to add permission you can also use - instead to remove it)
Where permissions can be:
r —> read
w—> write
x —>excecute
... and more
FOR EXAMPLE: it seems you write permissions for the first file so :
chmod +w /var/app/myapp
Will fix problem
I've created a simple bash script that grabs some data and then outputs it to a log file. When I run the script without sudo it fails to write to the logs and says they are write-protected. It then ask me if it should unwrite-protect them, but this fails (permission denied).
If I run the script as sudo it appears to work without issue. How can I set these log file to be available to the script?
cd /home/pi/scripts/powermonitor/
python /home/pi/powermonitor/plugpower.py > plug.log
echo -e "$(sed '1d' /home/pi/scripts/powermonitor/plug.log)\n" > plug.log
sed 's/^.\{139\}//' plug.log > plug1.log
rm plug.log
grep -o -E '[0-9]+' plug1.log > plug.log
rm plug1.log
sed -n '1p' plug.log > plug1.log
rm plug.log
perl -pe '
I was being dumb. I just needed to set the write permissions on the log files.
The ability to write a file depends on the file permissions that have been assigned to that file or, if the file does not exist but you want to create a new file, then the permissions on the directory in which you want to write the file. If you use sudo, then you are temporarily becoming the root user, and the root user can read/write/execute any file at all without restriction.
If you run your script first using sudo and the script ends up creating a file, that file is probably going to be owned by the root user and will not be writable by your typical user. If you run your script without using sudo, then it's going to run under the username you used to connect to the machine and that user will need to have permission to write the log files.
You can change the ownership and permissions of directories and files by using the chown, chmod, chgrp commands. If you want to always run your script as sudo, then you don't have much to worry about. If you want to run these commands without sudo, that means you're running them as some other user and you will need to grant write permission to that user, whoever it is, in order to write the files/folders where the log files get written.
For instance, if I wanted to run the script as user sneakyimp and wanted the files written to /home/sneakyimp/logs/ then I'd need to make sure that directory was writable by sneakyimp:
sudo chown -R sneakyimp:sneakyimp /home/sneakyimp/logs
This command changes ownership of that directory and its contents to the user sneakyimp. You might also need to run some chmod commands to make sure they are writable by owner.
For an application I want to store specific data on a data recovery server. On the application and DR server I created a user test1 and copied the public key from application server user test1 to DR user test1's authorized_keys file. User test1 is added to the wheel group.
I set permission on drwxr-xr-x /var/log
I then created a cron job to rsync the data from the application server to the DR server:
sudo rsync -avz -e "ssh -i /home/test1/.ssh/my-ssh-key" /var/nfsshare/ test1#10.10.10.10:/var/nfsshare > /var/log/nfs_cron-$(date +\%m-\%d-\%Y).log
When the cron executes I get the following error:
/bin/sh: /var/log/nfs_cron-08-26-2019.log: Permission denied
However, when I try to create a file manually it creates the file successfully.
sudo touch /var/log/test.txt
which creates the file as:
-rwxr-xr-x. 1 test1 test1 0 Aug 26 12:28 test.txt
Any thoughts?
Thanks!
You can create a directory and give permission to this user or you can use redirection/tee to write the log file.
For example using ACL:
mkdir -p /var/log/my_app/
setfacl -Rm g:MY_GROUP_ID:rwx /var/log/my_app/
The setfacl command is to setup ACL.
-R -> It's to be recursive and setup the ACL to all subfolder
-m -> It's to modify the ACL
goru -> It's to define the group or user
rwx -> It's the permission to setup for the group/user
http://tldp.org/LDP/abs/html/abs-guide.html#SETFACLREF
Another way is using redirection/tee. With redirection, you can "filter" what you want log into the file. For example:
Log and/or concatenate just in case of success
ls -lZ /tmp/myfile >> /var/log/mylog
Log everything (Sending stderr to stdout and writing into the same file)
ls -lZ /tmp/myfile >> /var/log/mylog 2>&1
or just use &>
ls -lZ /tmp/myfile &> /var/log/mylog
If you don't have permission to write on the destination file/directory, you can use tee to write. For example, appending (-a) and writing into the file /var/log/mylog.
ls -lZ /tmp/myfile | sudo tee -a /var/log/mylog
You can find some other examples and a better explanation in here:
https://www.tldp.org/LDP/abs/html/io-redirection.html
https://wiki.bash-hackers.org/howto/redirection_tutorial
https://wiki.bash-hackers.org/syntax/redirection
The problem is that the redirection is done by the user calling sudo, rather than by the sudo itself.
cmd > file
creates file before launching cmd, which means in your case that the regular user is trying to create the log, and then pass its filehandle to sudo to write to.
To confirm my theory, try this:
sudo echo test > /var/log/test.txt
and you should get the same error message.
You have to pass the filename to the command so that it is created by the program called by sudo. In your case, you could accomplish this by wrapping the whole thing into a script, for example.
I want to give 777 permission for the files in UNIX and change that file as DOS file.
I want to achieve this in shellscript file. I will pass the partial file name from command prompt. Example: if the file name is employeesalary, employeejob then if i pass employee in the command prompt then all the file which starts with employee will be given access to 777 and also it needs to be changed as DOS file.
filename={$1}
chmod 777 $filename*
u2d -i $filename*
When i run the above code i am getting the below error.
chmod: WARNING: can't access employee*
can't open employee*: No such file or directory in some location it specified
But when i run these commands alone in command prompt its working fine
chmod 777 employee*
u2d -i employee*
There's no need for a separate variable here. Just do
chmod 777 "$1"* && u2d -i "$1"*
If you prefer it as three lines:
filename="$1"
chmod 777 "$filename"* || exit $?
u2d -i "$filename"*
That said, 777 (world-writable, world-executable) is probably not a good idea. Would 755 (rwxr-xr-x) or even 644 (rw-r--r--) work for you? If so, that would be better.
Shell script file dummy.sh with -rw-r--r-- permission, runs fine with below commands.
/usr/bin/sh dummy.sh
(OR)
/usr/bin/bash dummy.sh
But ends up with bash: ./dummy.sh: Permission denied error, when executed directly as below. What's the reason behind this?
./dummy.sh
Your Script needs to be marked as executable for your system. This is done by setting the "x" bit for either the owner, the group or the rest of the world.
See: Wikipedia - Unix permissions
By executing
chmod 755 dummy.sh
you will set read, write and execute permissions for the owner of the script and read and execute permissions for the group and the rest of the world.
Provide "execute" permission to your shell script, using either of the following options:
chmod 744 dummy.sh
chmod u+x dummy.sh
Do refer to chmod documentation