-1

I have a question about ulimit:

ulimit -u unlimited
ulimit -n 60000

If I execute these in a screen, will they forever be kept as a setting on the screen until I kill the screen or do I have to run it every time I run the program?

What I want to do is irrelevant, I just want to know if they will be kept as a setting within the screen.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Are you referring to the [screen(1)](http://man7.org/linux/man-pages/man1/screen.1.html) utility in your question, or to command windows? – Basile Starynkevitch Nov 04 '17 at 14:20
  • yes I am referring to screen in linux that you can type screen -r nameofscreen – user7157477 Nov 04 '17 at 14:38
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Nov 04 '17 at 15:33

1 Answers1

5

ulimit is a bash builtin. It invokes the setrlimit(2) system call.

That syscall modifies some limit in its -shell- process (likewise the cd builtin calls chdir(2) and modifies the working directory of your shell process).

In a bash shell, $$ expands to the pid of that shell process. So you can use ps $$ (and even compose it, e.g. like in touch /tmp/foo$$ or cat /proc/$$/status)

So the ulimit applies to your shell and stay the same until you do another ulimit command (or until your shell terminates).

The limits of your shell process (and also its working directory) are inherited by every process started by fork(2) from your shell. These processes include those running your commands in that same shell. Notice that changing the limit (or the working directory) of some process don't affect those of the parent process. Notice that execve(2) don't change limits or working directories.

Limits (and working directory) are properties of processes (not of terminals, screens, windows, etc...). Each process has its own : limits and working directory, virtual address space, file descriptor table, etc... You could use proc(5) to query them (try in some shell to run cat /proc/self/limits and cat /proc/$$/maps and ls -l /proc/self/cwd /proc/self/fd/). See also this. Limits (and working directory) are inherited by child process started with fork(2) which has its own copy of them (so limits are not shared, but copied ... by fork).

But if you start another terminal window, it is running another shell process (which has its own limits and working directory).

See also credentials(7). Be sure to understand how fork(2) and execve(2) work, and how your shell uses them (for every command starting a new process, practically most of them).

You mention kill(1) in some comments. Be sure to read its man page (and every man page mentioned here!). Read also kill(2) and signal(7).

A program can call by itself setrlimit(2) (or chdir(2)) but that won't affect the limits (or working directory) of its parent process (often your shell). Of course it would affect future fork-ed child processes of the process running that program.

I recommend reading ALP (a freely downloadable book about Linux programming) which has several chapters explaining all that. You need several books to explain the details.

After ALP, read intro(2), be aware of existing syscalls(2), play with strace(1) and your own programs (writing a small shell is very instructive; or study the code of some existing one), and read perhaps Operating Systems: Three Easy pieces.

NB. The screen(1) utility manages several terminals, each having typically its shell process. I don't know if you refer to that utility. Read also about terminal emulators, and the tty demystified page.


The only way to really kill some screen is with a hammer, like this: breaking a screen with a hammer

(image of a real hammer hitting a laptop found with Google, then cut with gimp, and put temporarily on my web server; the original URL is probably https://www.istockphoto.com/fr/photo/femme-daffaires-stress%C3%A9-%C3%A0-lordinateur-crash-arrive-et-d%C3%A9truisent-le-moniteur-gm172788693-5836585 and I understand the license permits me to do that.)

Don't do that, you'll be sorry.

Apparently, you are talking of sending a signal (with kill(1) or killall(1) or pkill(1) to some process running the screen(1) program, or to its process group. It is not the same.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you for the big comment, but I am sorry to say that I am not very good in this field and its alot of this I dont understand, what I got a tip from a friend was to put ulimit -n 60000 and ulimit -c unlimited so my program dont restart when exceeding a userlimit of default of 1000. But without sounding condecending do you mind telling me if shell = screen and that aslong as i dont kill the screen, the settings remain? if it dont remain, where do you suggest me to put the limits without reseting? thanks and I hope you dont take offense to my answer – user7157477 Nov 04 '17 at 14:40
  • That is not a comment, but an answer. You need to read more about processes (and about `fork` and `execve`); I added reference to the ALP book, and you need to understand more about processes and shells – Basile Starynkevitch Nov 04 '17 at 14:42
  • I am sorry, it is sure an answer but as I said, I don't understand that so good and I don't have the time to read in because this has to happen today. My question is pretty simple and that is all I want to know. I tried to disconnect from putty and enter the screen and the settings seemed to remain, but leaving the screen is default ulimit settings but in screen that i resume i have the settings i originally putted which is good, so I wonder if it will look for the screen settings for ulimit or the default root when leaving sceen – user7157477 Nov 04 '17 at 15:00
  • 2
    You need to spend several days reading more about processes and shells. I gave many links in my answer, but you need to do the learning job. It seems you don't understand what is a Unix process, and it takes time to understand that in full. I spent months on that in the late 1980-s. And explaining what exactly is a process takes several chapters of a book like ALP. I don't have the patience and the space to explain all that, you need to learn. – Basile Starynkevitch Nov 04 '17 at 15:01
  • As long as `screen` keeps your shell process running, that shell keeps its limits, unless you change them with `ulimit`. Once that shell is gone, the limits don't exist (because they belong to the shell process) – Basile Starynkevitch Nov 04 '17 at 15:05
  • Now I start to get it more, but can a shell process be gone and a screen remain? or does it only go away if a screen is killed? – user7157477 Nov 04 '17 at 15:17
  • You can terminate a shell process with `exit` builtin, or kill it with a command like `kill $$`. I'm confused by a "a screen is killed". Do you mean "a `screen` process is killed"? AFAIK, you can only kill processes (and related). I guess you are stil uneasy with the notion of process. You need to take time to read more about it. – Basile Starynkevitch Nov 04 '17 at 15:18
  • what i mean with if a screen is killed is if i type the command killall screen, it will terminate all the screens i got available, then im pretty sure the ulimit will reset if i start a new screen, but i wonder if i dont kill the screen and changed the ulimit settings, is there any way the ulimits can reset if i dont kill the screen? – user7157477 Nov 04 '17 at 15:22
  • You don't kill screens. You might kill processes running (or started by) the [screen(1)](http://man7.org/linux/man-pages/man1/screen.1.html) utility. It is not the same. You can kill other processes too. – Basile Starynkevitch Nov 04 '17 at 15:24
  • And you are misunderstanding: `killall screen` is killing all the processes running the `screen` utility. AFAIK the only way of killing screens is to use a hammer on your own LCD display device (or laptop). Don't do that, you'll be sorry. The `kill` (and `killall`) command can only send signals to processes (and to process groups) – Basile Starynkevitch Nov 04 '17 at 15:30
  • I spent a few minutes finding and cutting an image about killing screens. Do you understand that it is not the same as killing a process? – Basile Starynkevitch Nov 04 '17 at 16:12