Executing a shell script on a udev rule - java

I am trying to execute a Java program on inserting a USB Flash Drive and pass the path of the USB Flash Drive as a parameter to my Java program. To achieve this, I have written a 'udev' rule in both '/etc/udev/rules.d' and '/lib/udev/rules.d' with the names '99-thumbdrive.rules' and '98-thumbdrive.rules'.
The rule is as follows:
ACTION=="add", KERNEL=="sd?1", SUBSYSTEM=="usb", RUN="/home/nouman/Data/NSProj/shell.sh"
The 'shell.sh' file has been made executable by using 'sudo chmod +x shell.sh'
When I insert my Flash Drive and run the command 'udevadm test /dev/bus/003/008', I see my rules being read.
read rules file: /lib/udev/rules.d/98-thumbdrive.rules
read rules file: /etc/udev/rules.d/99-thumbdrive.rules
My shell.sh file is as follows
#!/bin/bash
java USB
I have been trying this for a couple of days now and have read most of the posts online but am unable to run my script. Kindly help me out.

Related

JMeter get the latest file / contents from SFTP folder

I am trying to use JMeter to perform an end to end test. The test involves writing to SFTP folder and reading a file generated as a result of write operation from another SFTP folder.
I am able to connect to SFTP folder using JMeter SSH SFTP plugin and able to successfully write / read SFTP folder contents.
The application under test, creates an output file based on the input file (put by JMeter). The challenge now I have is to read the contents / file which is created on the SFTP folder.
The application under test writes a file with an date-time string which JMeter may not know hence I am trying to read the latest file.
The JMeter SSH SFTP plugin provides a number of options i.e. ls, rm, rmdir, etc however, I have chosen the edit option (${sftp username#servername 'ls -ltr /server/path | tail -n 1'}) and tried to use the following in order to read the file however, I neither see error nor response.
I would appreciate any pointers and if you can think of a better solution. Please also let me know if you would like me to share more information.
Thanks in advance.
You're using the wrong sampler, if you want to run a command you need (surprisingly) SSH Command Sampler
This pipe symbol | is not a parameter for ls command, it's a part of Unix shell, in majority of cases it would be bash so you need to amend your command to look like:
/bin/bash -c "ls -ltr /server/path | tail -n 1"
example SSH Command Sampler configuration:
and example output:
More information: How to Run External Commands and Programs Locally and Remotely from JMeter

Translating a Jar-opening Batch file into its MacOS Equivalent

Hello again Stack Overflow!
I am currently working on a Java program that is essentially a digitized character sheet for a Dungeons and Dragons style adventure. I'm currently learning how to use IntelliJ IDEA, and while I haven't been able to figure out how to create a standalone executable .jar file, I have been able to find a workaround in the form of a .bat file that I have nicknamed rubberglove.bat (because if you can't open a jar, you use a...).
However, as in my previous post, I've run into a problem, as one of my new players uses a Mac, and since I don't know if it's possible to run rubberglove.bat in a non-Windows environment, I'll probably have to translate it into something MacOS can understand. However, I've never owned a Mac, so I'm not sure what the file extension even is, let alone what to put inside.
The contents of rubberglove.bat are shown below:
java -jar [program_name].jar
What would the Mac equivalent of this file and the commands inside? Thanks in advance for all your help!
Create a file called rubberglove (or rubberglove.sh, the file suffix is optional). And then set the execute bit. Something like
$ cat << EOF > rubberglove
#!/usr/bin/env bash
java -jar [program_name].jar
EOF
$ chmod +x rubberglove
$ ./rubberglove
Error: Unable to access jarfile [program_name].jar
Adjust "[program_name]" as needed.

How do I make my directory give the executable permissions to script files which are going to be created by my Java program?

I want my directory to give executable permissions (by default) to all the shell scripts which are going to be created in that directory after I run my Java program. I tried the following commands:
setfacl -d -m group:name:rwx /path/to/your/dir
find ./ -name "*.sh" -exec chmod +x {} \;
The first one is not showing any response while the second one works fine but I have to execute this command manually in terminal after my Java program has created all the scripts. This is not what i seek. I want this thing to be automatic. Here is what I am trying to achieve:
My Java program creates the .sh files in a directory.
Now the program would try to execute this script file.
Here is a Java code snippet which shows how it is going to execute the script files:
ExecuteShellComand obj = new ExecuteShellComand();
String command2 = "./script.sh";
String output2 = obj.executeCommand(command2);
It doesn't run unless I give the executable permissions to the script.sh. How do I do it? Is there any way around it? If I am not doing something in a way it should be done, feel free to give your suggestions. Thanks
Default ACL permissions are masked by the file's creation mode as specified in open and mkdir syscalls.
Since files are usually created with a default mode of 0666, execute permissions will be masked instead of inherited.
It's the responsibility of the program that creates the files to create them with the right permissions. If you set the permissions correctly when creating the scripts, you won't need ACL or chmod at all. The best way to fix this would be for your program to set the mode in the open call to 0777.
Java appears to have Files.createFile for this. Additionally, you have a more fuzzy File.setExecutable to do it after the fact, which is not Unix canonical behavior, but probably fine for your use case.

Run Java class file in PHP (running successfully in command line but not PHP)

I know the approach to run external programs in PHP is to use exec() or shell_exec(). I have tried them both, even system(), but none of these seem to be working.
I have added all the required jar files to my "jre/ext" folder and running the java class file is successful in command line.
So in command line, after compiling, my command to run is "java MyProgram". In PHP, I tried echo exec('java MyProgram') and shell_exec('java MyProgram').
I have a simple sample Hello_World Java file and PHP is able to run that successfully. I am thinking may be the issue can be I have Apache POI jar files that are needed for the java class, but I don't know how should I resolve or explore further now.
Thanks in advance!

Runtime exec output path

I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.

Categories

Resources