sreda, 14. november 2012

Running sudo command without password

What I found out is that running a command using sudo without entering password there are two options:

  • echo PASSWORD | sudo -S COMMAND
  • adding following lineto /etc/sudoers:
username ALL=(ALL) NOPASSWD: COMMAND
Please note that this has to be after all group definitions.
You can check what sudo priviliges you have using
sudo -l
For this case this gives:
(ALL) ALL
(ALL) NOPASSWD: COMMAND 
This means that all command require password except for the COMMAND. If we would put the username definitin before group definitions sudo -l would give:
(ALL) NOPASSWD: COMMAND
(ALL) ALL
This would still require password for all commands.

sreda, 07. november 2012

Find where code was changed in Mercurial

If you want to find out in which revisions a piece of code was changed then you can use a Mercurial command something like this:

hg grep --all PIECE_OF_CODE | awk 'BEGIN {FS=":"} {print $1" "$2}' | uniq
and you will get back a list of files with coresponding revisuin numbers:
repo/src/java/com/kovica/File1.java 30
repo/src/java/com/kovica/File1.java 10

torek, 06. november 2012

Remove empty lines from a file on Linux using command line

This one is especially useful when searching through Glassfish's log files.
There are many ways you can remove empty lines from a file:

  • awk 'NF' file
  • sed '/^$/d' file
  • grep "^$" -v file
  • grep . file
  • egrep "^[[:space:]]?$" -v file
  • perl -n -e 'print unless /^\s*$/' file
And I'm sure there are many more. :)

torek, 09. oktober 2012

Convert author in Mercurial repositiories

This is a summary of StackOverflow question http://stackoverflow.com/questions/12136895/change-the-author-in-mercurial-history


- cd repo1/.hg
- vi .hgrc and add
        [extensions]
        hgext.convert=
- cd $HOME
- vi authors.convert.list
        USER1 =USER2
- hg convert --authors $HOME/authors.convert.list repo1 repo1.NEW

Merge two mercurial repositories into one

This is a summary of a StackOverflow question http://stackoverflow.com/questions/12109203/merge-different-repositories-into-one


cd repo2
mkdir repo2
hg rename * repo2
hg commit -m "Move repo2 files into a subfolder"
cd repo1
hg pull -f path\to\repo2
hg merge
... and deal with merge conflicts if any ...
hg commit -m "Merge with repo2"

Glassfish: Run asadmin from Java program

When you execute asadmin on a secure enabled server you have to provide admin username and password. This can be a bit of a PITA if you are calling asadmin from a Java program.
You have two options:

  1. Create $HOME/.asadminpass:
    asadmin://admin@localhost:4848 BASE64_ENCODED_PASSWORD
  2. Or you can pass the file with the password with every asadmin command:
    1. Create glassfisg.password file:
      AS_ADMIN_PASSWORD=PLAIN_ADMIN_PASSWORD
    2. If you are on Linux it is a good idea to make the file readable only to your user:
      chmod 600 glassfish.password
    3. Execute asadmin command:
      $com.sun.aas.installRoot/bin/asadmin --user admin --passwordfile PATH_TO_PASSWORD_FILE list-applications
$HOME is a home directory of a user under which Glassfish is running.
$com.sun.aas.installRoot is the installation directory of Glassfish. It is only set on a Glassfish process.

OpenMQ: Create queues from Java program

When you execute imqcmd command from OpenMQ you have to provide username and password, which is a bit of a PITA if you are calling the imqcmd from Java programs.
This is the recipe you can use to get around this problem:

  1. Create a file calles imqCmd.password with following content:
    imq.imqcmd.password=ADMIN_OPENMQ_PASSWORD
  2. If you are on Linux it is a good idea to make the file readable only to your user:
    chmod 600 imqCmd.password
  3. Run the commands for queue creation:
    $com.sun.aas.imqBin/imqcmd create dst -n MyQueue -t q -o "maxNumMsgs=1000" -o "limitBehavior=FLOW_CONTROL" -o "maxNumMsgs=-1" -o "localDeliveryPreferred=false" -o "useDMQ=true" -o "validateXMLSchemaEnabled=false" -u admin -passfile PATH_TO_PASSWORD_FILE
  4. You can also delete the queue:
    $com.sun.aas.imqBin/imqcmd destroy dst -n MyQueue -t q -f -u admin -passfile PATH_TO_PASSWORD_FILE
com.sun.aas.imqBin is a System property that tells you where you can find imqcmd and other OpenMQ executables. It is only set on a Glassfish process.

Interesting Glassfish system properties


Properties:

  • com.sun.aas.imqBin: here you find executables from OpenMQ
  • com.sun.aas.agentRoot: here you find directory for nodes
  • com.sun.aas.productRoot: installation directory of Glassfish
  • com.sun.aas.configRoot: directory where Glassfish keeps config files
  • com.sun.aas.imqLib: library directory of OpenMQ
  • com.sun.aas.domainsRoot: directory of Glassfish domains
  • com.sun.aas.instanceRoot: directory of the current domain

ponedeljek, 24. september 2012

suspend/hibernate from command line

If you want to put your computer to hibernate from command line you have several options:
  1. echo disk | sudo tee /sys/power/state
  2. pm-hibernate
  3. pmi action hibernate #have to do sudo apt-get install powermanagement-interface before using it
If you want to put your computer to suspend from command line you have several options:
  1. echo mem | sudo tee /sys/power/state
  2. pm-suspend
  3. pmi action suspend #have to do sudo apt-get install powermanagement-interface before using it

nedelja, 23. september 2012

Toggle touchpad under Linux

If you want to toogle touchpad device under Linux, you have to do this:
sudo apt-get install unclutter

Put following lines into toggleTouchpad.sh:
          #!/bin/bash
deviceName="AlpsPS/2 ALPS DualPoint TouchPad"
isEnabled=`xinput --list-props "$deviceName" | grep -e "Device Enabled.*1$"`
if [ -n "$isEnabled" ]; then
    echo Disabling touchpad $deviceName
    xinput --set-prop "$deviceName" "Device Enabled" 0
    unclutter &
else
    echo Enabling touchpad $deviceName
    killall unclutter
    xinput --set-prop "$deviceName" "Device Enabled" 1
fi
 
Name of my touchpad device is AlpsPS/2 ALPS DualPoint TouchPad. You can find the list of all input devices issuing
xinput list
then you just have to guess what device it is :)
Then you can link this bash script to a keyboard shortcut and you are done.

There is also another way of doing this. Issu following command
synclient  TouchpadOff=1
If this command disables your touchpad then toogleTouchpad.sh might look like this:
#!/bin/bash

isEnabled=`synclient | grep -e ".*TouchpadOff.*0$"`

if [ -n "$isEnabled" ]; then
    echo Disabling device
    synclient TouchpadOff=1
    unclutter &
else
    echo Enabling device
    killall unclutter
    synclient TouchpadOff=0
fi
 

četrtek, 13. september 2012

Ubuntu 12.04.1 LTS and "Waiting for network configuration"

If you are running an Ubuntu 12.04.1 server and you see "Waiting for network configuration" message during boot and then another one "Waiting for 60 moreseconds for network configuration" then you have udev configured wrongly.

I got this error while cloning a VirtualBox VM and reinitializer MAC address for the network card. But if you have physical server, you probably changed network cards, ...

The solution is to check the file /etc/udev/rules.d/70-persistent-net.rules and see if you have more entries that you should have or the name of the subsystem is not the same as the network interface configured in /etc/network/interfaces or the MAC address is not right.

sreda, 05. september 2012

Theme change

I think it is time to change appearance of this blog. :)
What do you think?

sreda, 29. avgust 2012

JellyBean with multiple users

If you have a rooted Android phone running Jelly Bean, then you can have multiple users, just like on a regular Linux machine. :)
If you want to add a user then open terminal emulator and type:

  1. su
  2. pm create-user foo
Press and hold the power button and you'll be able to select your newly created user.
If you want to list all users on the system then open terminal emulator and tpe:
  1. su
  2. pm list-users
If you want to remove a user then open terminal emulator and type:
  1. su
  2. pm list-users and remember the number before user foo
  3. pm remove-user 1

pm list-users gives something like:
Users: 
                UserInfo{0:Primary:3}
                UserInfo{1:foo:3}

petek, 24. avgust 2012

Prevent user from running su

If you want to prevent user foo from running command su then do this:

  1. sudo grouadd nosu
  2. sudo usermod -a -G nosu foo
  3. sudo vi /etc/pam.d/su and uncomment
    auth required pam_wheel.so deny group=nosu

sobota, 18. avgust 2012

Required property "db.example.com" is unknown host. ERRORCODE=-4222, SQLSTATE=08001

If DB2 JCC driver throws mentioned error this means that it cannot resolve db.example.com. You should check your DNS settings.

sreda, 25. julij 2012

NetBeans 7.2

NetBeans 7.2 is out!
Go grab it while it's hot. :)
New&Noteworthy page can be found here.

I hope you'll enjoy it as much as I do!

ponedeljek, 23. julij 2012

com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException

If you get 
com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2030][11211][4.13.80] A communication error occurred during operations on the connection's underlying socket, socket input stream, or socket output stream.  Error location: Reply.fill() - insufficient data (-1).  Message: Insufficient data.. ERRORCODE=-4499, SQLSTATE=08001

then this usually means you are using a connection pool to get connections to database and that connections in the connection pool are "stale".
What does stale mean?
You have connection pool started, connections created, but then database restarts. The connection you now get out of the connection pool are now stale and you have to restart the connection pool (close all connections and create new ones).


nedelja, 27. maj 2012

Sourcing .bashrc in non-interactive shell

This is a new post in a long, long time. A lot has happened since New Year. I'll post about that at a later date. :)

Now to using bash under Linux.
When you are running a bash session in a terminal you are inside of an interactive environment and you can edit .basrc and source it at any given time.
You source .bashrc like this:
source ~/.bashrc or . ~/.bashrc
Now, let's say you have a bash script that you want to run. You can run in in a terminal, but that is not the subject of this post. You can run it via a file manager, like Dolphin, thunar, ... or you can put a shortcut to it on your desktop.
If you want to source .bashrc in that script, it won't work, because running via file manager or desktop is running a bash script in an non-interactive environment. .bashrc notices that and does not do anything. In fact if you look at .bashrc you will find something like this:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
at the beginning of you .bashrc.
But there is a solution for this. Put PS1='$ ' in your script before sourcing .bashrc.
Examples:
- run a terminal
- edit .bashrc and put 
export TEST_TEXT='My test text' in your .bashrc
- make a new script test.sh:
#!/bin/bash
. ~/.bashrc
echo "TEST_TEXT = $TEST_TEXT"
read
- make it executable
chmod +x test.sh
- run it and you should get something like this:
TEST_TEXT = 
- now source .bashrc and run the test.sh script
. ~/.bashrc
./test.sh
- and you get
TEST_TEXT = My test text
If you run this script via file manager you will always get TEST_TEXT = 
Change the script to 
#!/bin/bash
PS1='$ '
. ~/.bashrc
echo "TEST_TEXT = $TEST_TEXT"
read
and .bashrc will get source everytime you run it.

nedelja, 01. januar 2012

2012

Let 2012 be a special one.
My New Year's resolution was also to blog more ofter with some interesting dtuff about Linux, version control, Java, ....