/proc directory script - ruby

I'm looking for a ruby script that accesses the /proc directory and saves the process ID and command line (cmdline) information in a file.

you may want to call ps instead of going to /proc.
cmd=`ps -eo pid,cmd`
o = File.open("output","w")
o.write(cmd)
o.close

you can also run below one liner bash script and redirect its output anywhere, as well as choose required argument option for head command.
ls -alR /proc/$(ls /proc/ |grep -i '[0-9]'|sort -n|head ) > /proc_open_files

Related

Is there a way to find all the files that got created a specific time?

I want to make a bash script in unix where the user gives a number between 1 and 24.then it has to scan every file in the directory that the user is and find all the files that got created the same time as the number.
I know that unix wont store teh birth time for most of the files.So I found that each file has crtime, which you can find with this line of code: debugfs -R 'stat /path/to/file' /dev/sda2
The problem is that i have to know every crtime so I can search them by the hour.
thanks in advance and sorry for the complicated explenation and bad english
Use a loop to execute stat for each file, then use grep -q to check whether the crtime matches the time given by the user. Since paths in debugfs -R 'stat path' do not necessarily correspond to paths on your system and there might be quoting issues, we use inode numbers instead.
#! /usr/bin/env bash
hour="$1"
for f in ./*; do
debugfs -R "stat <$(stat -c %i "$f")>" /dev/sda2 2> /dev/null |
grep -Eq "^crtime: .* 0?$hour:" &&
echo "$f"
done
Above script assumes that your working directory is on an ext4 file systems on /dev/sda2. Because of debugfs you have to run the script as the superuser (for instance by using sudo). Depending on your system there might be an alternative to debugfs which can be run as a regular user.
Example usage:
Print all files and directories which were created between 8:00:00 am and 8:59:59 am.
$ ./scriptFromAbove 8
./some file
./another file
./directories are matched too
If you want to exclude directories you can add [ -f "$f" ] && in front of debugfs.
The OP has already identified the issue that Unix system calls do not retrieve the file creation time - just the modification times (either mtime or ctime). Assuming this is acceptable substitution, an EFFICIENT way to find all the files in the current directory created on a specific hour is to leverage 'ls' and an awk filter.
#! /bin/sh
printf -v hh '%02d' $1
ls -l --time-style=long-iso | awk -v hh=$hh '+$7 == hh'
While this solution does not access the actual creation time, it does not require super user access (which is usually required for debugfs)

Using Pipe in Make File

I'm trying to create a make command that uses a pipe. But the command is not executing as expected.
Example
SHELL:=/bin/bash
all:
$(shell ps aux | grep -i someProcName)
Output:
bash: kls602: command not found
grep: kls602: No such file or directory
grep: 46905: No such file or directory
grep: 0.0: No such file or directory
grep: 0.0: No such file or directory
grep: 4306668: No such file or directory
grep: 6480: No such file or directory
grep: s008: No such file or directory
grep: S: No such file or directory
grep: 12:22PM: No such file or directory
grep: 0:00.57: No such file or directory
...
It's as if the output of ps is being taken in as the file to search in the grep command. I can't figure out what I'm doing wrong.
So how do you write semi complex bash command in make? I would prefer a better answer than "Just run a bash script instead of putting the command in a makefile".
Using $(shell...) in a recipe is an anti-pattern. You should never do it except in the most unusual, bizarre situations. A recipe IS a shell script, so running $(shell ...) there is, at best, redundant.
Second, read what the shell function does:
The shell function performs the same function that backquotes (‘`’) perform in most shells: it does command expansion. This means that it takes as an argument a shell command and evaluates to the output of the command.
So your understanding of what's happening is exactly correct, and that's exactly what it's supposed to do.
You should write this simply as:
all:
ps aux | grep -i someProcName

How to create a file with its name starting with dash in Linux? (ex "-file")

How can I create a file named "-file" using command line in Linux?
specify a path in front of it, e.g. ./-file
In bash -- is a flag that is interpreted as "nothing after this should be taken as a flag", so - is no longer parsed as an option.
touch -- -file
ls -ltr | awk '$NF ~ /^--/{print "rm ./" $NF}'|sh

unable to access UNC drive from Bash Script .sh

Trying to access network drive (UNC) from the bash script. The network drive needs username and password.
i Can access UNC by running some individual commands like CD, net use .However unable to execute from a script.
follows below steps
1) Mount the drive to x drive by using below command from
Command : net use x: \\\\Server_name\\Directory /user:users pass /PERSISTENT:YES
Result :Sucess mounted x drive
2) test.sh
#!/bin/bash
ls /cygdrive/x
count_node1 = cat a.log b.log.1 v.log.2 |grep "&&&&" | sort -k1,2 | grep -c 'word'
#count_node1="got it"
echo helloworld
echo $count_node1
#end
Result: helloWorld
: No such file or directory/x
count_node1: command not found
3)Further, If I run each line from Cygwin individually it is working perfectly.
trying bash profile for first time really confused.
The order matter with net use:
net use x: \\\\Server_name\\Directory pass /user:users
See syntax of your shell (bash). Correct:
count_node1=$(cat a.log b.log.1 v.log.2 |grep "&&&&" | sort -k1,2 | grep -c 'word')
or
count_node1=$(grep '&&&&' a.log b.log.1 v.log.2 |sort -k1,2 | grep -c 'word')
Remove the carriage return characters from the ends of your script's lines, i. e. save it in unix instead of dos file format.

Can I use a variable in a file path in bash? If so, how?

I'm trying to write a small shell script to find the most recently-added file in a directory and then move that file elsewhere. If I use:
ls -t ~/directory | head -1
and then store this in the variable VARIABLE_NAME, why can't I then then move this to ~/otherdirectory via:
mv ~/directory/$VARIABLE_NAME ~/otherdirectory
I've searched around here and Googled, but there doesn't seem to be any information on using variables in file paths? Is there a better way to do this?
Edit: Here's the portion of the script:
ls -t ~/downloads | head -1
read diags
mv ~/downloads/$diags ~/desktop/testfolder
You can do the following in your script:
diags=$(ls -t ~/downloads | head -1)
mv ~/downloads/"$diags" ~/desktop/testfolder
In this case, diags is assigned the value of ls -t ~/downloads | head -1, which can be called on by mv.
The following commands
ls -t ~/downloads | head -1
read diags
are probably not what you intend: the read command does not receive its input from the command before. Instead, it waits for input from stdin, which is why you believe the script to 'hang'. Maybe you wanted to do the following (at least this was my first erroneous attempt at providing a better solution):
ls -t ~/downloads | head -1 | read diags
However, this will (as mentioned by alvits) also not work, because each element of the pipe runs as a separate command: The variable diags therefore is not part of the parent shell, but of a subprocess.
The proper solution therefore is:
diags=$(ls -t ~/downloads | head -1)
There are, however, further possible problems, which would make the subsequent mv command fail:
The directory might be empty.
The file name might contain spaces, newlines etc.

Resources