LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2017, 04:03 AM   #1
hypogogix
LQ Newbie
 
Registered: Nov 2017
Posts: 2

Rep: Reputation: Disabled
Cool Writing a script to renice another script using pidof and renice


So I go to this really poorly delivered Uni class on Linux.

Anyways, one of the questions in the abysmal labs we were given asks us to write a script called 'cruncher' which increments numbers indefinitely so that we can see it running in top...

it then asks you to write a bash script using $1 and $2 (which I believe are variables) to find the <pid> of the cruncher script and renice it using the <pid> as it's running so we can see it change.

so I wrote something along the lines of...

#!/bin/bash

$1 pidof cruncher ;$2 renice $1 19;

I know this isn't right but its quite an ambiguous question and so finding the answer isn't that simple. I've spent more time cluelessly looking for answers than actually learning in this course and this is my last resort. Please help. My sanity depends on it lol

Thanks very much in advance.

Cheers

A linux newb

Hypogogix
 
Old 11-08-2017, 07:11 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,037
Blog Entries: 13

Rep: Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486Reputation: 3486
Hi hypogogix and welcome to LQ.

$1, $2, and so forth are passing arguments to the script, and this is the standard convention used to indicate those arguments. When you call your script, say it is called test.sh, if you type, "./test.sh this that", the string this is in the variable $1 and the string that is in the variable $2. So if you want to give the name for a process, such as cruncher, you would give that as an argument to your script, therefore allowing you to change that name without changing your script.

You would probably use the first argument $1 to get the pid of cruncher. "pidof $1" where cruncher was your first passing argument.

Similarly, you can choose a level of nice as your second argument, $2.

Here are some links to review to understand how to use bash scripting, the beginners guide will be very helpful:

Bash Guide for Beginners
Advanced Bash Scripting Guide

Also, here is a Bash guide I wrote to help bash programmers with debugging their scripts:

Bash Scripting for Dummies and Geniuses

For when you post any additional code clips, it is helpful to use [code][/code] tags around your code, or in Advanced Edit mode, to use the # tool to do this same action. This will both identify your code, as in the example below, and also maintain formatting of the code.
Code:
#!/bin/bash

set -xv

echo "This is a simple bash programming example
 
1 members found this post helpful.
Old 11-08-2017, 07:13 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,026

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Welcome to LinuxQuestions.

While we are happy to help you with specific questions we will not do your labwork/homework for you. I will help you with your first question/problem.

$1,$2 are known as positional parameters or command line arguments. See below for a quick tutorial.
http://wiki.bash-hackers.org/scripting/posparams
https://bash.cyberciti.biz/guide/How...nal_parameters

What rtmistler posted...
 
Old 11-08-2017, 11:41 AM   #4
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,552

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Actually running this cruncher program will use 100% of a single CPU machine.
So other processes wouldn't get a look-in, no matter what the niceness was.

You might try something (very inefficient!) like
Code:
#! /bin/bash

if [[ $# -eq 1 ]]; then
   PGM="$1"
else
   PGM="cruncher"
fi

ACTIVE="YES"

while [[ 0 -ne 1 ]]; do
sleep 4

HOT=$(inxi -s | grep 'System Temperatures' | cut -b 66-67)

if [ $ACTIVE=="YES" ] && [[ $HOT -gt 51 ]]; then
    killall -s STOP "$PGM"
    ACTIVE="NO"
fi

if [ $ACTIVE=="NO" ] && [[ $HOT -lt 35 ]]; then
    killall -s CONT "$PGM"
    ACTIVE="YES"	
fi
done
This causes "cruncher" to be suspended if the temperature of the CPU > 51 degrees.
(You need to check that the temperature is monitored properly though).
 
Old 11-12-2017, 04:07 PM   #5
hypogogix
LQ Newbie
 
Registered: Nov 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys. The labs I have are horrendous and I have no idea what I'm doing. I get the idea of scripting in my head as a quick batch file but I don't know how to do very simple things.

one of the questions is about tar and I'm having a nightmare with this. The thing that bothers me mostly is that it IS VERY SIMPLE and I just can't find the right information to show me. Some examples to work with like I get cp $1 $2 would be fine for a tiny copy script for example...

I am trying to write a script that tars a directory... here's the question. I can hardly make sense of it...

Write a shell script that produces a zipped-tar version of any input
directory called ‘dir_name’, whereby ‘dir_name’ represents the relative
directory name, not the absolute one. The output should be send to /tmp or
a floppy device and should be of the form: dir_name.tar.gz .

nano tarscript...

#!/bin/bash

I don't understand how I can assign a variable "dir_name" to do this. I have like a week to learn this and right now isn't the time to go back to basics and get a book for shell scripting when I have three other really comprehensive exams to prepare for in a week or so, so it would be really helpful to me if someone could just give me the answer and I'll work back with it to understand it.

Forgive me for asking but no one in University seems interested in helping. I have only encountered bash scripting very recently. It's fine if it's just a batch file using commands but when it comes to assigning variables to part of those commands like a file name (or path) it just bamboozles me. I'm giving myself a crash course in JavaScript and finding the time for bash scripting just isnt there so if it comes to it I'd rather drop marks here than a whole other subject.

Many thanks in to all of you who replied. I found it all helpful.
 
Old 11-12-2017, 04:21 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,552

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
<deleted>

Last edited by JeremyBoden; 11-12-2017 at 04:35 PM.
 
Old 11-13-2017, 12:38 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
please re-read post #3:
Quote:
Originally Posted by michaelk View Post
While we are happy to help you with specific questions we will not do your labwork/homework for you.
i think you would benefit from going through a bash tutorial.
write a few simple scripts to get a feel for how everything works.
many results if you search for it.
here's a good, albeit dated one: http://tldp.org/LDP/Bash-Beginners-Guide/html/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing kernel module to renice processes DKSL Linux - Newbie 2 06-02-2012 10:48 PM
what's your favourite renice value? alaios Linux - General 3 07-21-2011 09:37 AM
[SOLVED] Non-root renice mwkemo Linux - Newbie 3 09-02-2009 12:38 AM
Renice a process yenonn Linux - General 2 10-03-2005 07:59 PM
about renice the process Rex_chaos Linux - General 5 08-01-2004 06:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration