Running scripts on startup/shutdown @Ubuntu

Hi,

As I’m using RxTx libraries for serial communication and I couldn’t handle the locks and permissions on the ports when Tomcat closes( please comment if you know:)), I need to remove locks and give appropriate permissions to ports on startup. Today I saw a post @  http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/ and I tried it on my pc. I’m using Ubuntu 11.10.

First write your script and make it executable and move it to /etc/init.d/.

$ chmod +x /home/gokceng/Desktop/myScript.sh

$ sudo cp /home/gokceng/Desktop/myScript.sh /etc/init.d/

$ sudo update-rc.d  myScript.sh start 99 2 3 4 5 .

The parameters on the last line:

myScript.sh : your script to run

start : When your script run, it can be  stop

99 : On start in this scenario, it makes your script to run after everything. It could be 1 for being first or anything between 1 and 99.

2 3 4 5 : run levels of your  script

. : Don’t forget to put this 🙂

My script is like that, it is useless but it is only for explanation:

### BEGIN INIT INFO
# Provides: myScript
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#!/bin/bash
sudo chmod 666 /home/gokceng/Desktop/myTest

The lines between BEGIN INIT INFO and END INIT INFO is for getting “missing LSB information” error. You can get more information about these lines from http://wiki.debian.org/LSBInitScripts

For more compact way of running your script both on startup and shutdown you can also use this:

update-rc.d yourScript start 20 2 3 4 5 . stop 20 0 1 6 .

You can see other usages check this link: http://translated.by/you/man-update-rc-d-8-install-and-remove-system-v-style-init-script-links/original/

I hope it helps you.

Java Serial Programming – Checking Ports

I’m developing an application on android and I need to read from my tablet’s usb port. I’m using a rs232 to usb  chip because  my other device sends data from its serial port and my tablet has no serial port. As you already know whenever you unplug your usb/serial device from your computer and then plug it again, usually its port number changes. For example before unplugging your device the port number was ttyUSB1 but re-plugging it makes the number ttyUSB7 or whatever. I couldn’t find anyway to stop that. If you know a solution for this, please inform me.

You can list plugged devices’ port using Java. I’ll provide sample code for this:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;

public class PortFinder
{
 public class Driver
 {
 public Driver(String name, String root)
 {
 mDriverName = name;
 mDeviceRoot = root;
 }

 private String mDriverName;
 private String mDeviceRoot;
 Vector<File> mDevices = null;

 public Vector<File> getDevices()
 {
 if (mDevices == null)
 {
 mDevices = new Vector<File>();
 File dev = new File("/dev");
 File[] files = dev.listFiles();
 int i;
 for (i = 0; i < files.length; i++)
 {
 if (files[i].getAbsolutePath().startsWith(mDeviceRoot))
 {
 System.out.println("Found new device: " + files[i]);
 mDevices.add(files[i]);
 }
 }
 }
 return mDevices;
 }

 public String getName()
 {
 return mDriverName;
 }
 }

 private Vector<Driver> mDrivers = null;

 Vector<Driver> getDrivers() throws IOException
 {
 if (mDrivers == null)
 {
 mDrivers = new Vector<Driver>();
 LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
 String l;
 while ((l = r.readLine()) != null)
 {
 // Issue 3:
 // Since driver name may contain spaces, we do not extract
 // driver name with split()
 String drivername = l.substring(0, 0x15).trim();
 String[] w = l.split(" +");
 if ((w.length >= 5) && (w[w.length - 1].equals("serial")))
 {
 System.out.println("Found new driver " + drivername + " on " + w[w.length - 4]);
 mDrivers.add(new Driver(drivername, w[w.length - 4]));
 }
 }
 r.close();
 }
 return mDrivers;
 }

 public String[] getAllDevices()
 {
 Vector<String> devices = new Vector<String>();
 // Parse each driver
 Iterator<Driver> itdriv;
 try
 {
 itdriv = getDrivers().iterator();
 while (itdriv.hasNext())
 {
 Driver driver = itdriv.next();
 Iterator<File> itdev = driver.getDevices().iterator();
 while (itdev.hasNext())
 {
 String device = itdev.next().getName();
 String value = String.format("%s (%s)", device, driver.getName());
 devices.add(value);
 }
 }
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 return devices.toArray(new String[devices.size()]);
 }

 public String[] getAllDevicesPath()
 {
 Vector<String> devices = new Vector<String>();
 // Parse each driver
 Iterator<Driver> itdriv;
 try
 {
 itdriv = getDrivers().iterator();
 while (itdriv.hasNext())
 {
 Driver driver = itdriv.next();
 Iterator<File> itdev = driver.getDevices().iterator();
 while (itdev.hasNext())
 {
 String device = itdev.next().getAbsolutePath();
 devices.add(device);
 }
 }
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 return devices.toArray(new String[devices.size()]);
 }

 public static void main(String args[])
 {
 PortFinder portFinder = new PortFinder();
 String[] devices = portFinder.getAllDevices();
 String[] devicePaths = portFinder.getAllDevicesPath();
 for (int i = 0; i < devices.length; i++)
 {
 System.out.println("Device:->" + devices[i] + " Path:->" + devicePaths[i]);
 }
 }
}
 

Using this you will be able to operate (change permission, close, open, write, read etc) on your correct port without looking at /dev.

And the output is(Pay attention to Device:->ttyUSB0 (usbserial) Path:->/dev/ttyUSB0)

Found new driver usbserial on /dev/ttyUSB
Found new driver rfcomm on /dev/rfcomm
Found new driver serial on /dev/ttyS
Found new device: /dev/ttyUSB0
Found new device: /dev/ttyS31
Found new device: /dev/ttyS30
Found new device: /dev/ttyS29
Found new device: /dev/ttyS28
Found new device: /dev/ttyS27
Found new device: /dev/ttyS26
Found new device: /dev/ttyS25
Found new device: /dev/ttyS24
Found new device: /dev/ttyS23
Found new device: /dev/ttyS22
Found new device: /dev/ttyS21
Found new device: /dev/ttyS20
Found new device: /dev/ttyS19
Found new device: /dev/ttyS18
Found new device: /dev/ttyS17
Found new device: /dev/ttyS16
Found new device: /dev/ttyS15
Found new device: /dev/ttyS14
Found new device: /dev/ttyS13
Found new device: /dev/ttyS12
Found new device: /dev/ttyS11
Found new device: /dev/ttyS10
Found new device: /dev/ttyS9
Found new device: /dev/ttyS8
Found new device: /dev/ttyS7
Found new device: /dev/ttyS6
Found new device: /dev/ttyS5
Found new device: /dev/ttyS4
Found new device: /dev/ttyS3
Found new device: /dev/ttyS2
Found new device: /dev/ttyS1
Found new device: /dev/ttyS0
Device:->ttyUSB0 (usbserial) Path:->/dev/ttyUSB0
Device:->ttyS31 (serial) Path:->/dev/ttyS31
Device:->ttyS30 (serial) Path:->/dev/ttyS30
Device:->ttyS29 (serial) Path:->/dev/ttyS29
Device:->ttyS28 (serial) Path:->/dev/ttyS28
Device:->ttyS27 (serial) Path:->/dev/ttyS27
Device:->ttyS26 (serial) Path:->/dev/ttyS26
Device:->ttyS25 (serial) Path:->/dev/ttyS25
Device:->ttyS24 (serial) Path:->/dev/ttyS24
Device:->ttyS23 (serial) Path:->/dev/ttyS23
Device:->ttyS22 (serial) Path:->/dev/ttyS22
Device:->ttyS21 (serial) Path:->/dev/ttyS21
Device:->ttyS20 (serial) Path:->/dev/ttyS20
Device:->ttyS19 (serial) Path:->/dev/ttyS19
Device:->ttyS18 (serial) Path:->/dev/ttyS18
Device:->ttyS17 (serial) Path:->/dev/ttyS17
Device:->ttyS16 (serial) Path:->/dev/ttyS16
Device:->ttyS15 (serial) Path:->/dev/ttyS15
Device:->ttyS14 (serial) Path:->/dev/ttyS14
Device:->ttyS13 (serial) Path:->/dev/ttyS13
Device:->ttyS12 (serial) Path:->/dev/ttyS12
Device:->ttyS11 (serial) Path:->/dev/ttyS11
Device:->ttyS10 (serial) Path:->/dev/ttyS10
Device:->ttyS9 (serial) Path:->/dev/ttyS9
Device:->ttyS8 (serial) Path:->/dev/ttyS8
Device:->ttyS7 (serial) Path:->/dev/ttyS7
Device:->ttyS6 (serial) Path:->/dev/ttyS6
Device:->ttyS5 (serial) Path:->/dev/ttyS5
Device:->ttyS4 (serial) Path:->/dev/ttyS4
Device:->ttyS3 (serial) Path:->/dev/ttyS3
Device:->ttyS2 (serial) Path:->/dev/ttyS2
Device:->ttyS1 (serial) Path:->/dev/ttyS1
Device:->ttyS0 (serial) Path:->/dev/ttyS0

Running Shell Commands With Java As Root

For a long time I’ve been searching for running sudo commands without password prompt in Java. I’ve seen a post by Saeid Zebardast and thanks to him I solved that issue.

First you need  to edit /etc/sudoers file:

$ sudo gedit /etc/sudoers

and add that lines to this file:

# for user
YOUR_USER_NAME ALL= NOPASSWD: ALL

# for group
YOUR_GROUP_NAME ALL= NOPASSWD: ALL

Although the first line is worked for me, Saeid suggests adding both.

Anyway, after that change you can run your sudo command within Java:

String command = "sudo chmod 777 /home/gokceng/Desktop/test.my";
 Runtime runtime = Runtime.getRuntime();
 try
 {
 Process process = runtime.exec(command);
 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
 String line;
 while ((line = bufferedReader.readLine()) != null)
 {
 System.out.println(line);
 }
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }