[help] Writing to /opt/local/var/log/install_setup.log ?

Bill Hernandez ms at mac-specialist.com
Fri May 2 16:36:28 PDT 2008


I am trying to write a running command log to : /opt/local/var/log/ 
install_setup.log
but doing either below :

shell> $ sudo echo "${TS} $1" >> $BASH_CMD_LOG
shell> $ sudo su -m root echo "${TS} $1" >> $BASH_CMD_LOG

do not work...

This all works very well for me if I write to a dir not owned by root,  
so I generally write basic command/install log stuff to non root  
directories, but I would rather write to /opt/local/var/log/ which  
happens to be owned by root.

The other alternative was to write the log to /tmp/install_setup.log  
and move it to /opt/local/var/log/ when I am done, or change the  
permissions, but I'd rather just write to the log from the script  
'test_one()' without all the little workarounds.

sudo does not work for me writing to the log, however sudo works fine  
for all other commands. I guess it's all part of the security issues  
in trying to protect the logs ?

These are the permissions :

shell> $ ls -ls /opt/local/var/ | grep log
0 drwxr-xr-x  10 root  admin  340 May  2 15:58 log

shell> $ ls -ls /opt/local/var/log | grep install_setup
8 -rw-r--r--  1 root  admin  168 May  2 15:58  
install_setup_[2008.05.02](03.58.PM).log


Using the single step process below wont work, and generates errors as  
shown in 'test_one' below.

shell> $ test_one
Password:

test_one: line 12: /opt/local/var/log/install_setup.log: Permission  
denied
test_one: line 13: /opt/local/var/log/install_setup.log: Permission  
denied

test_one: line 12: /opt/local/var/log/install_setup.log: Permission  
denied
test_one: line 13: /opt/local/var/log/install_setup.log: Permission  
denied

shell> $

Using the two step process below works fine but I dont want to do  
everything as root, I just want to write to the log and not have to  
change the permissions for the files/dirs.

shell> $ sudo su -m root
sh-3.2#> test_two
# ----> SUCCESS
sh-3.2#> exit
exit
shell> ~ $

# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# script_name : test_one
# called : shell> $ test_one
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
#!/bin/bash

BASH_CMD_LOG=/opt/local/var/log/install_setup.log
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
function bh_log( )
{
	TS=$(date +\[%Y.%m.%d\]\(%I.%M.%p\))
	sudo echo "${TS} $1" >> $BASH_CMD_LOG  # Generates line 12 Permission  
denied
	sudo su -m root echo "${TS} $1" >> $BASH_CMD_LOG  # Generates line 13  
Permission denied
}
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
sudo echo
bh_log "MacPorts Setup Start..."
# <snip> ... </snip>
bh_log "MacPorts Setup Finish..."
open ${BASH_CMD_LOG}

# END OF SCRIPT ( test_one )
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+


# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# script_name : test_two
# called step ( 1 ) : shell> $ sudo su -m root
# called step ( 2 ) : sh-3.2#> test_two
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
#!/bin/bash

BASH_CMD_LOG=/opt/local/var/log/install_setup.log
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
function bh_log( )
{
	TS=$(date +\[%Y.%m.%d\]\(%I.%M.%p\))
	echo "${TS} $1" >> $BASH_CMD_LOG
}
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
sudo echo
bh_log "MacPorts Setup Start..."
# <snip> ... </snip>
bh_log "MacPorts Setup Finish..."
open ${BASH_CMD_LOG}

# END OF SCRIPT ( test_two )
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+

Anybody know how to write to the log using test_one() without having  
to alter file/dir permissions, or use any of the alternatives  
mentioned above...

TIA

Bill Hernandez
Plano, Texas


More information about the macports-users mailing list