LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-12-2016, 06:55 PM   #1
dlb101010
Member
 
Registered: Dec 2016
Posts: 61

Rep: Reputation: 18
/bin/systemctl overload


This is on a Debian machine:
$ uname -r
debian 3.16.0-4-amd64

The following /sbin commands still work as expected, at least halt, reboot and shutdown do. So I'm curious how systemctl knows which one is being executed as they all link to the same command.

$ ls -l | grep systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 halt -> /bin/systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 poweroff -> /bin/systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 reboot -> /bin/systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 runlevel -> /bin/systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 shutdown -> /bin/systemctl
lrwxrwxrwx 1 root root 14 Nov 23 13:24 telinit -> /bin/systemctl

Thanks!
 
Old 12-12-2016, 07:07 PM   #2
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
systemctl changes its behavior depending on how it is being called.
 
Old 12-12-2016, 08:12 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
With a bit more explanation - systemctl determins its function based on the first parameter (shell value $0) after stripping off the path that may be included. The symbolic links permit the invocation of the execl (see manpage on execl). The first parameter is conventionally the path to the image to execute.

from the manpage on execl:
Quote:
The execv(), execvp(), and execvpe() functions provide an array of
pointers to null-terminated strings that represent the argument list
available to the new program. The first argument, by convention,
should point to the filename associated with the file being executed.
The array of pointers must be terminated by a null pointer.
 
1 members found this post helpful.
Old 12-13-2016, 01:58 AM   #4
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 363Reputation: 363Reputation: 363Reputation: 363
Great to see your 'deep' Linux activity! (ref. your 1st IntroForum ThreadPost)

Look into how busybox works: it has either soft or hard links to hundreds like `ls`
$0 is whatever you invoked it as. Try this: create a script called name:
Code:
echo 'echo I will do $0 but I am just plain name' > name 
chmod 755 name; cat name 
ln -s name name1; ln name name2; ls -li name* 
./name1 
./name2
(./ needed if . not in $PATH)
Note name2 is a 'hard' link: same inode# as name! Thus 'physically' same file!

Best wishes! Enjoy always!
p.s. here's a recent 'deep' book you might like; 11.3.4 mentions $0; 2.9 $PATH

Last edited by Jjanel; 12-13-2016 at 07:46 PM.
 
1 members found this post helpful.
Old 12-13-2016, 02:12 AM   #5
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 6,885
Blog Entries: 17

Rep: Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135Reputation: 4135
This logic applies to scripts as well as programs. In a bash script, the calling name is stored in $0. That's how sysv init scripts work. There is a directory for each run level in /etc/rc.d with links to the actual scripts in /etc/rc.d/init.d. Links whose names begin with "S" start services and links beginning with "K" kill them.

The first thing the scripts always do is to check the name under which they were called. Then they run in start or stop mode accordingly.
 
1 members found this post helpful.
Old 12-13-2016, 08:32 AM   #6
dlb101010
Member
 
Registered: Dec 2016
Posts: 61

Original Poster
Rep: Reputation: 18
Thanks all for the detailed explanation! The execl behavior explains it. And Jjanel, your script was a nice demonstration...if a picture is worth a thousand words, a working code example is worth two thousand. At least! (Also, thanks for suggesting "How Linux Works" Looks promising.)
 
1 members found this post helpful.
  


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
systemctl vs init.d on Centos 7 Ryanms3030 Linux - Newbie 8 11-04-2014 07:55 PM
is systemctl here to stay? zQUEz Linux - General 3 11-18-2012 06:55 AM
Fedora 16 systemctl error Pierredup Linux - Server 3 04-15-2012 12:25 PM
sshd : redirecting to systemctl mohamed makhyoun Linux - Software 5 01-16-2012 12:25 AM
echo $PATH = /home/g3rc4n/bin:/usr/local/bin:/usr/bin:/bin:/usr/games ? i_heart_pandas Linux - Software 7 09-18-2009 08:33 AM

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

All times are GMT -5. The time now is 10:38 AM.

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
Open Source Consulting | Domain Registration